From bug-request at octave dot org Wed Apr 5 14:28:43 2006 Subject: Plot should be able to handle empty vector inputs From: "John W. Eaton" To: Quentin Spencer Cc: octave bug mailing list Date: Wed, 5 Apr 2006 15:28:27 -0400 On 5-Apr-2006, Quentin Spencer wrote: | In Matlab, it is possible to call the plot function with empty data. | This is an error | >> plot([]) | ??? Error using ==> plot | Vectors must be the same lengths. | | However, this is acceptable. | >> plot([],[]) | | | In octave, I get the following error: | oct 1> plot([],[]) | error: __plt2__: invalid data for plotting | error: evaluating if command near line 48, column 3 | error: called from `__plt2__' in file | `/usr/share/octave/2.9.4/m/plot/__plt2__.m' | error: evaluating if command near line 58, column 4 | error: evaluating if command near line 56, column 2 | error: evaluating if command near line 55, column 7 | error: evaluating while command near line 44, column 5 | error: evaluating if command near line 34, column 3 | error: called from `__plt__' in file | `/usr/share/octave/2.9.4/m/plot/__plt__.m' | error: called from `plot' in file `/usr/share/octave/2.9.4/m/plot/plot.m' | | Supporting this behavior is useful because I have a situation where I'm | plotting several vectors, and one pair of them may be empty, but I don't | know in advance without extra code to check for it. As you can see, this | is version 2.9.4, but looking at CVS, it looks like this hasn't changed | since then. kHow about the following change. I think it does what you want, but I've not done a lot of testing. | I might be able to write a patch for this, but I don't have | time right now--maybe later if I'm the first one to get to it. Oops, sorry. Well, instead of a patch, can you at do some testing of the one below? Thanks, jwe scripts/ChangeLog: 2006-04-05 John W. Eaton * plot/__plt2__.m: Return data = [] and fmtstr = "" if x1 and x2 are both empty. * plot/__plt__.m: Ignore empty data and format strings returned from __plt1__ or __plt2__. Index: scripts/plot/__plt2__.m =================================================================== RCS file: /cvs/octave/scripts/plot/__plt2__.m,v retrieving revision 1.15 diff -u -r1.15 __plt2__.m --- scripts/plot/__plt2__.m 8 Sep 2005 01:40:58 -0000 1.15 +++ scripts/plot/__plt2__.m 5 Apr 2006 19:25:16 -0000 at @ -67,6 +67,9 @@ else error ("__plt2__: invalid data for plotting"); endif + elseif (isempty (x1) && isempty (x2)) + data = []; + fmtstr = ""; else error ("__plt2__: invalid data for plotting"); endif Index: scripts/plot/__plt__.m =================================================================== RCS file: /cvs/octave/scripts/plot/__plt__.m,v retrieving revision 1.36 diff -u -r1.36 __plt__.m --- scripts/plot/__plt__.m 13 Oct 2005 17:51:19 -0000 1.36 +++ scripts/plot/__plt__.m 5 Apr 2006 19:25:16 -0000 at @ -56,11 +56,15 @@ if (x_set) fmt = __pltopt__ (caller, next_arg); if (y_set) - [__plot_data__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}{j}, fmtstr] = __plt2__ (x, y, fmt); + [tdata, tfmtstr] = __plt2__ (x, y, fmt); else - [__plot_data__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}{j}, fmtstr] = __plt1__ (x, fmt); + [tdata, tfmtstr] = __plt1__ (x, fmt); + endif + if (! isempty (tdata)) + __plot_data__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}{j} = tdata; + fmtstr = tfmtstr; + have_data = true; endif - have_data = true; x_set = false; y_set = false; else at @ -69,8 +73,12 @@ elseif (x_set) if (y_set) fmt = __pltopt__ (caller, ""); - [__plot_data__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}{j}, fmtstr] = __plt2__ (x, y, fmt); - have_data = true; + [tdata, tfmtstr] = __plt2__ (x, y, fmt); + if (! isempty (tdata)) + __plot_data__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}{j} = tdata; + fmtstr = tfmtstr; + have_data = true; + endif x = next_arg; y_set = false; else at @ -105,10 +113,10 @@ __plot_data_offset__{__current_figure__}(__multiplot_xi__,__multiplot_yi__) = j; - if (! isempty (__plot_command__{__current_figure__}{__multiplot_xi__,__multiplot_yi__})) - if (__multiplot_mode__) - __gnuplot_raw__ ("clear\n"); - endif + if (__multiplot_mode__) + __gnuplot_raw__ ("clear\n"); + endif + if (! strcmp (__plot_command__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}, "__gnuplot_plot__")) eval (__plot_command__{__current_figure__}{__multiplot_xi__,__multiplot_yi__}); endif ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------