# Plotting a variable against loop iteration

4 Ansichten (letzte 30 Tage)
Alla am 17 Aug. 2017
Kommentiert: Alla am 18 Aug. 2017
I have a variable changing within a loop and I want to plot it against the loop's iteration in real time, sounds simple enough but I just can't get it to work, I get an empty plot with the Y axis changing without having anything plotted.
for i=900:1:nfiles
%%Code that generates variable (Y) here %%
x = linspace(0,nfiles);
plot(x,Y,'LineWidth',2,...
'MarkerEdgeColor','b',...
'MarkerFaceColor','b',...
'MarkerSize',10);
drawnow
end
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Alla am 18 Aug. 2017
That would definitely work, but what I need is to plot the results in real time.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Robert U am 18 Aug. 2017
Hi Alaa MAZOUZ,
The marker-property of plot is by default 'none'. Choose a marker.
x is - according to the presented example - not dependent on i, thus, you do not calculate it repeatedly in every loop.
I like it better to open one parent figure which can be modified and draw the results into it. That might be useful for you, too. You might want to fix the limits of your axes to see Y moving in a fixed frame instead of seeing the frame moving for a quite fixed point (cloud).
x = linspace(0,nfiles);
ah = axes; % open parent figure
for i=900:1:nfiles
%%Code that generates variable (Y) here %%
plot(ah,x,Y,'LineWidth',2,...
'Marker','.',... % set marker to filled point
'MarkerEdgeColor','b',...
'MarkerFaceColor','b',...
'MarkerSize',10);
drawnow
end
Kind regards,
Robert
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Alla am 18 Aug. 2017
Bearbeitet: Alla am 18 Aug. 2017
Thank you Robert that was quite helpful, I have managed to make it work but only after I had one other figure that displays other results within the loop omitted, basically when the code switched between the two figures mid loop the plot does not hold on to the plotted points and restarts each time giving me dots,to work around this I have tried to have all of the results displayed in one figure and avoid the switching all together I but couldn't manage that either.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on 2-D and 3-D Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by