How do I plot separate graphs in a for loop?
64 次查看(过去 30 天)
显示 更早的评论
Hello. I'm fairly new to matlab and I was experimenting with "for" loops today. I get the result I want, but each line is on the same graph. How do I get it to start a new graph each time so I would have three graphs with one line on it, instead of one graph with three lines?
for A=[1;2;3]
for B=[1;2;3]
t=linspace(1,3);
ans=A+B;
plot(t,ans*t)
end
end
0 个评论
回答(1 个)
Dyuman Joshi
2023-3-14
编辑:Dyuman Joshi
2023-3-14
To iterate over the values of a column vector, you need to transpose it to create a row vector and then proceed.
Otherwise, the loop index will be equal to the column vector, see below
vec=[1;2;3];
%column vector as index
for idx=vec
idx
end
%column vector transposed
for jdx=vec'
jdx
end
for A=[1;2;3]'
for B=[1;2;3]'
t=linspace(1,3);
ans=A+B;
figure
%Random color for each graph
color=rand(1,3);
plot(t,ans*t,'Color',color)
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!