0

I mean to print the value of a variable myvar of unknown type, concatenated with arbitrary text, without a newline.

I know how to print without a newline: printf('My var = %<format specifier>, is it ok for you?', myvar), but this requires knowing the type of myvar.

I know how to print the value of a variable of unknown type: disp(myvar), but this always prints a newline (besides the fact I wouldn't know how to concatenate via disp).

How would I get My var = <any variable>, is it ok for you?<no newline> printed?

  • Does “unknown type” include custom classes with overloaded `disp` method? – Cris Luengo Aug 09 '20 at 13:24
  • @CrisLuengo - In principle, yes. But just including builtin types (albeit possibly complex, since nested structures, e.g., are also possible) would be quite good. For instance, I want to use this on the values of all properties for an arbitrary object, so they can be quite varied. – sancho.s ReinstateMonicaCellio Aug 09 '20 at 14:59

1 Answers1

0

You can't really do that. Octave just doesn't really work like that.

What you could do, however, is have prepared sprintf-generated strings, which you can plug into your output. E.g.

var1 = 123;    % double
var2 = 'abc';  % char
var3 = cell(); % cell

Var = var2;   % choose from var1, var2, or var3

switch class(Var)
  case {'double'}, FormattedStr = sprintf('Value is %d', Var);
  case {'char'}  , FormattedStr = sprintf('Value is %s', Var);
  case {'cell'}  , % ... etc
  otherwise      , FormattedStr = sprintf('Value is unknown');
end

fprintf( 'As I said before: %s', FormattedStr);

Alternatively, you can create a "toString" function that tries to detect the type and return an appropriate string representation.

If you really want to use the output of disp, then in theory you could use fdisp to write to a file, and then read back from that file and process the newlines out accordingly. But I think this is overkill.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • All options are overly complex for a seemingly simple action. Having a `-nonewline` flag for `disp` (or the like) seems so simple... – sancho.s ReinstateMonicaCellio Aug 10 '20 at 19:09
  • This is because `disp` is a _display_ function. It would not make sense. What you need is a "string formatting" function. Which assumes you want to format your variable in a particular way. Not knowing the type of what you're printing is a pretty impossible scenario. What are you trying to do exactly? – Tasos Papastylianou Aug 10 '20 at 19:35
  • I don't see it impossible and, in fact, there a quite a few languages that allow for this (including Octave with `disp`!). What I mean to do is exactly what is asked. Printing in a way that I don't have to care about the type, and being able to continue printing on the same line. I would trust the language to provide a good-enough format, exactly what I do with `disp`. I can always write a wrapper to catch exceptions to this rule in case the format provided is not-that-good. But I would be writing a mere zero to a-few such rules. I cannot figure out how to explain it differently. – sancho.s ReinstateMonicaCellio Aug 10 '20 at 21:01