MATLAB - patch update in gui?

Getting started with the Matlab guide, click on the stumbling block. Got it as simple as possible, like a gui toy, to illustrate my problem. Gui (named asas) has a button and an axis. Button callback reads

axesHandle= findobj(gcf,'Tag','axes1'); x=rand(randi(10+20,1),4); plot(axesHandle, x) 

There is no other code written by me (the manual wrote it).
The first time I press the button, everything is in order: the plot ends. The second time, I get an error message from the console:

 Error using plot Vectors must be the same lengths. Error in asas>pushbutton1_Callback (line 83) plot(axesHandle, x) Error in gui_mainfcn (line 96) feval(varargin{:}); etc... 

I want to build new data x, replacing the old one.
It seems that Matlab does not replace the data with the plot, but somehow tries to add to the plot?

I searched, but did not find anything that is applicable.

+4
source share
1 answer

The explanation is not simple - and, of course, not if you are new to MATLAB and its graphics subsystem.

Your code as is, line by line:

 axesHandle= findobj(gcf,'Tag','axes1'); x=rand(randi(10+20,1),4); plot(axesHandle, x); 

The first line tries to find in the current figure ( gcf , "get current figure") any child with the 'Tag' property set to the string 'axes1' . Think you know that? The second line, of course, generates some random data to build. The third line displays the data in x .

But after plot -call the 'Tag' property is actually reset to '' (empty string), which in turn makes findobj unsuccessful for any subsequent searches for the axis descriptor. The axesHandle variable, therefore, DOES NOT contain the actual descriptor, but instead an empty matrix [] . This will make the default graph different; it interprets the empty matrix as data for the x-axes (the first argument to plot ). This will result in the error you get:

 ... Error using plot Vectors must be the same lengths. ... 

Dan's solution in the comment above is a workaround, but it makes sense to tell plot where to build, especially in graphical interfaces.

Instead, you can add a fourth line:

 set(axesHandle,'Tag','axes1'); 

This will return the 'Tag' property to 'axes1' , and subsequent clicks on the button will also work. And now you can add multiple axis objects. If that is what you want.

+8
source

All Articles