Plotting a variable against loop iteration
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 17 de Ag. de 2017
Editada: Stephen23
el 17 de Ag. de 2017
How many values of Y are calculated on each iteration? By this description "...the Y axis changing without having anything plotted" it sounds like you are plotting one point at a time.
In any case, the best solution will be to store all of the data in one array and then plot it after the loop.
Respuesta aceptada
Robert U
el 18 de Ag. de 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 comentarios
Robert U
el 18 de Ag. de 2017
Example for a point drawing a circle:
clear
ah = axes;
for i = 0:360
x(i+1) = sind(i);
Y(i+1) = cosd(i);
plot(ah,x(1:end-1),Y(1:end-1),...
'LineWidth',1);
hold on
plot(ah,x(end),Y(end),...
'LineWidth',2,...
'Marker','.',...
'MarkerEdgeColor','r',...
'MarkerFaceColor','r',...
'MarkerSize',24);
axis equal
ah.XLimMode = 'manual';
ah.XLim = [-1 1];
ah.YLimMode = 'manual';
ah.YLim = [-1 1];
hold off
drawnow;
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Graphics Performance en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!