필터 지우기
필터 지우기

How do I plot separate graphs in a for loop?

조회 수: 18 (최근 30일)
Joseph Sikora
Joseph Sikora 2023년 3월 14일
편집: Dyuman Joshi 2023년 3월 14일
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

답변 (1개)

Dyuman Joshi
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
idx = 3×1
1 2 3
%column vector transposed
for jdx=vec'
jdx
end
jdx = 1
jdx = 2
jdx = 3
Use figure command to create new figure window, otherwise your plot will be overwritten.
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

카테고리

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