Hold on with subplot in a loop

67 次查看(过去 30 天)
Teshome Kumsa
Teshome Kumsa 2022-5-15
Hello,
Hold on, command is messing up my life. I was trying to plot figures using a loop. I want the first two loops in one subplot and the next two loops in another subplot. I tried to hold off and it didn't work.
A={[40;50;60] [70;80;90] [20;30;50]};
B={[45;55;65] [75;85;95] [25;35;55]};
T=[1;2;3];
for i=1:length(A)
figure(i)
subplot(2,1,1)
plot(T,A{i},'-r')
hold on
plot(T,B{i},'--b')
xlabel('x')
ylabel('y')
end
for i=1:length(A)
figure(i)
subplot(2,1,2)
plot(T,cumtrapz(A{i}),'-r')
hold on
plot(T,cumtrapz(B{i}),'--b')
xlabel('xdot')
ylabel('ydot')
end
hold off
for i=1:length(A)
figure(i)
subplot(2,1,1)
loglog(T,A{i},'-y')
hold on
loglog(T,B{i},'--g')
xlabel('ftx')
ylabel('fty')
end
for i=1:length(A)
figure(i)
subplot(2,1,2)
loglog(T,cumtrapz(A{i}),'-y')
hold on
loglog(T,cumtrapz(B{i}),'--g')
xlabel('ftxdot')
ylabel('ftydot')
end

回答(1 个)

VBBV
VBBV 2022-5-15
编辑:VBBV 2022-5-15
A={[40;50;60] [70;80;90] [20;30;50]};
B={[45;55;65] [75;85;95] [25;35;55]};
T=[1;2;3];
figure(1) % use this outside of loop
for i=1:length(A)
subplot(2,1,1)
plot(T,A{i},'-r')
title('figure 1')
hold on
plot(T,B{i},'--b')
xlabel('x')
ylabel('y')
end
for i=1:length(A)
subplot(2,1,2)
plot(T,cumtrapz(A{i}),'-r')
hold on
plot(T,cumtrapz(B{i}),'--b')
xlabel('xdot')
ylabel('ydot')
end
hold off
figure(2) % same here too
for i=1:length(A)
subplot(2,1,1)
loglog(T,A{i},'-y')
title('figure ')
hold on
loglog(T,B{i},'--g')
xlabel('ftx')
ylabel('fty')
grid on
end
for i=1:length(A)
subplot(2,1,2)
loglog(T,cumtrapz(A{i}),'-y')
hold on
loglog(T,cumtrapz(B{i}),'--g')
xlabel('ftxdot')
ylabel('ftydot')
grid on
end
  2 个评论
VBBV
VBBV 2022-5-15
Use figure outside of loop. you're trying to replicate figure windows with as many as size of your matrix A,
Teshome Kumsa
Teshome Kumsa 2022-5-15
编辑:Teshome Kumsa 2022-5-15
Your program is good, but it will not help me if my length (A) is too long. Of course, the reason why I want to replicate figures with as many as the size of my matrix is due to the large amount of data I have to work with. The code I put here is just a sample. It is not my real data. Assume my length (A) is 20. Does it make sense to plot 20 data sets in a single plot?

请先登录,再进行评论。

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by