I have a simple issue that I could use some help with. I am preparing a loop where each cycle saves a plot in a matlab.graphics.chart. A simple code example:
x=[2 4 6 8];
y=[10 20 30 40];
plot1=plot(x,y);
How do I re-plot "plot1"? Thanks

7 Kommentare

KSSV
KSSV am 6 Sep. 2017
Why you are worried of replotting? already you have data in hand..use plot again..
JB
JB am 6 Sep. 2017
When I use "plot(plot1)" I get a "Error using plot - Invalid handle"
plot(x,y)
You can issue the command as many times as you want.
Or do you mean to get x and y data from an existing plot?
You could also copy graphic objects, but that's a roundabout way to go about things if you do have the data.
JB
JB am 6 Sep. 2017
Hi José. The thing is that I have a loop function which will run about 60 cycles and generate one plot for each run and store it in a data array without showing the plots. Then I want to be able to re-cal the specific existing plot data and replot it. Sorry for clumsy description, I am pretty new in matlab coding.
JB
JB am 6 Sep. 2017
or even better. I want to attach a handle to each plot, for instance handles.plot1, handles.plot2.... handles.plot60. is it possible?
José-Luis
José-Luis am 6 Sep. 2017
When you say generate one plot and store it in a data array, how do you do that?
Please show some example code.
JB
JB am 6 Sep. 2017
Here is an example:
x=[2 4 6 8; 3 6 9 12; 0 -2 -4 -6];
y=[10 20 30 40; 10 20 30 40; 10 20 30 40];
for K = 1 : 3
hold on
Plot=plot(x(K,:),y(K,:));
plotdata{K,1}=Plot;
end
Then I want to be able to call and plot one of the three generated plots in the "plotdata"

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Guillaume
Guillaume am 6 Sep. 2017

1 Stimme

JB, looking at the various comments in this question, I've got the feeling that you're not totally understand what it is that the plot function returns.
The plot function returns a handle to a line object (which you store in your plotdata variable in one of your comment). Now one of two things can happen:
  • that line object still exists in the figure: In that case, you can toggle its visibility, change its properties (colour, line style, etc.) or do all sorts of things with it, but in any case, it is still part of whichever figure you created it in.
  • that line object no longer exists, for example the figure has been closed or the plot was deleted because you plotted on the same figure without hold on. In that case, the handle that you've stored in your variable is no longer valid. It does not contain any information about the original plot. It's completely useless and there is no way to retrieve the original plot.
This is why your question is confusing. Either the plot still exists (it may be hidden), in which case there's nothing to do to replot (except maybe unhide the plot), or the plot no longer exists in which case what you've stored is now useless and you'll have to call the plot function to replot

1 Kommentar

JB
JB am 6 Sep. 2017
Hi Guillaume. You are absolutely right, it is my lack of matlab knowledge which makes it confusing. I thought that I could prepare each plot during the loop function and then call it later. Thanks a lot for the plot description.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KL
KL am 6 Sep. 2017
Bearbeitet: KL am 6 Sep. 2017

0 Stimmen

plot1 is the axis handle. "re-plotting" means plotting on the same figure window with the new data.
for iCount = 1:5
x = iCount*[2 4 6 8];
y = iCount*[10 20 30 40];
h1 = figure(1); %h1 is figure handle
plot1=plot(x,y); %plot1 is axis handle
end
If you want to retain the plot from previous loop, use hold on at the end of for loop

2 Kommentare

Birch
Birch am 6 Sep. 2017
Hi KL, thanks for your answer. The thing is that I have a loop with 60 cycles, generating one plot per cycle. I want to be able to call each specific plot afterwards. Is it possible to set a handle on each plot?
JB
JB am 6 Sep. 2017
Here is an example for three plots:
x=[2 4 6 8; 3 6 9 12; 0 -2 -4 -6];
y=[10 20 30 40; 10 20 30 40; 10 20 30 40];
for K = 1 : 3
hold on
Plot=plot(x(K,:),y(K,:));
plotdata{K,1}=Plot;
end
From this I want to be able to call one of the plots from the plotdata array and re-plot it. In my original array I have 60 different plots. Is it possible?

Melden Sie sich an, um zu kommentieren.

José-Luis
José-Luis am 6 Sep. 2017

0 Stimmen

Not sure this is what you mean:
numPlots = 10;
lH = cell(1,numPlots);
for ii = 1:numPlots
hold on;
lH{ii} = plot(rand(10,1));
lH{ii}.Visible = 'off';
end
lH{2}.Visible = 'on';

3 Kommentare

JB
JB am 6 Sep. 2017
We are getting very close. From your example is it then possible to display/call one of the other plots (for instance lH{4}) later in the GUI without running the loop again. THANKS for helping me out here.
José-Luis
José-Luis am 6 Sep. 2017
Bearbeitet: José-Luis am 6 Sep. 2017
As long as you have not destroyed the axes or figure, probably.
IMO it might be better to store the data and plot when needed.
I am not entirely sure I understand what you are trying to do.
JB
JB am 6 Sep. 2017
The overall goal is to have the user of the GUI to load a relatively large dataset by uigetfile. Then from a pushbutton the data will be loaded and a data array is generated from a loop function where one part of the stored data is the 60 times plot data. Then from the GUI the user should be able to call a specific plot by a checkmark activation. So if I can just either re-plot the stored data or put a specific handle on each plot then it should be possible.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

JB
am 6 Sep. 2017

Kommentiert:

JB
am 6 Sep. 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by