How do I produce graphs from 'for' loop?

조회 수: 7 (최근 30일)
Cai Chin
Cai Chin 2020년 11월 28일
댓글: Star Strider 2020년 11월 28일
Hi, I am using MATLAB R2020a on a MacOS. I have specified the production of a graph within a 'for' loop but when I run the code, only the graph for the last run of the loop is displayed. I don't want to store all the vectors for each loop due to storage constraints and instead, the values in the vector are overwritten with each run of the loop.
Is there any way of displaying the graphs for all runs of the loop rather than just the last one?
Any suggestions would be much appreciated. Thanks in advance.
for currentcycle = 1:length(number_cycles)
if currentcycle > 1
% If comparison between raw v and w co-ordinates and previously
% accepted exponentially weighted moving mean meets condition
current_expmean_v = (1 - 1/weight(currentcycle))*(previous_expmean_v) + (1/weight(currentcycle))*(values_v);
current_expmean_w = (1 - 1/weight(currentcycle))*(previous_expmean_w) + (1/weight(currentcycle))*(values_w);
plot(values_v, values_w, previous_expmean_v, previous_expmean_w);
xlabel('v (mV)')
ylabel('w (mV)')
title('2D phase space trajectory of current cycle and exponentially weighted trajectory of previous cycle');
previous_expmean_v = current_expmean_v;
previous_expmean_w = current_expmean_w;
% If condition is not met, previous_expmean is not updated but
% remains the same and warning message is presented
end
end

채택된 답변

Star Strider
Star Strider 2020년 11월 28일
Use the hold function.
figure
hold on
<LOOP>
hold off
If you are plotting single points in each loop iteration (not possible to determine that from the posted code), plot a marker (or use the scatter function) rather than expect a line, since lines are only drawn between elements of vectors with elements presented to the plot function in each call to it.
  댓글 수: 4
Cai Chin
Cai Chin 2020년 11월 28일
Thanks again! The 'figure' command works well, but the subplot one only produced one plot. A subplot figure would be more ideal though!
Star Strider
Star Strider 2020년 11월 28일
As always, my pleasure!
The subplot version should produce 1 figure window with 3 plots. It would be figure(4) with my code:
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by