Function figure arguments

  • OS: e.g. Linux Mint 20.1
  • Octave version: Version 6.1.0
  • Installation method: Built and installed from source code

I submitted a bug report complaining that g=figure(5) works as expected while g=figure 5 throws an error. The “bug” was closed and I received the following response:

This is expected behavior. Calling figure 6 is equivalent to figure("6").
Using the command syntax might set the ans variable (if the command has an
output argument). But it cannot be used in assignments. Use the function syntax instead: h = figure(7).

Well, okay. That creates at least two other problems.

  1. The documentation for the figure function explicitly states, “Figure numbers must be positive integers.” Therefore, figure(“6”) should error; isnumeric “6” ==> false.

  2. The Octave manual, section 11.13 Commands, states, “Commands are a special class of functions that only accept string input arguments.” This conflicts with the idea of using command syntax with the figure function because as stated above, figure() requires a numeric, not string, argument.

  3. Per the manual, h=figure 5 is equivalent to h=figure(“5”); therefore, for the latter to work while the former errors demonstrates non-equivalence.

figure.m contains the following code in its input validation:

    if (nargs == 1 && ischar (arg))
      arg = str2double (arg);
      if (isnan (arg))
        arg = varargin{1};
      endif
    endif

So, a string input is converted to double using str2double. That was probably added for compatibility with (old) Matlab scripts or functions that used that undocumented Matlab feature.
That behavior was unexpected for me, too. But I’m not sure if we should encourage using that strange syntax by documenting it…

I agree that the wording of section 11.13 Commands in the manual could be better. It could be clearer that command syntax and function syntax are only equivalent if there is no output argument. In fact, output arguments are not possible with command syntax.
Could you please propose better wording (here or in that bug report)?

Thanks.

This is a feature, not a bug. It was added because its usage was frequent and programmers are lazy. It’s just faster to type figure 5 to go to that figure rather than figure (5) with the extra shift key and pulling your fingers off the home row if you are a touch typist.