Subplot in loop just plotting the first y value?
이전 댓글 표시
for n=[2:5]
for c=[10,20,40,80]
figure(1)
subplot(1,4,n-1)
x=linspace(0,2*pi,c);
y=atan(x)
plot(y)
end
end
This loop creates four subloops in one window, so far so good. But spent the last eternity trying to get Matlab to plot four different y values, not just four copies of the first.
Pic:

댓글 수: 3
Adam
2016년 9월 6일
You are calling the plot command 16 times there, in 4 bunches of 4, but the last of each 4 calls is always the same ( c == 80 ) so I'm not quite sure what you are trying to do with the double loops
Stephen23
2016년 9월 6일
This is a good example of why bad code formatting makes it hard to write good code: there are actually two loops, and this is the cause of the problem... however the nested loop is not clear because if the lack of indentation.
Use clear, consistent formatting and it makes writing, reading, and understanding code one million times easier. MATLAB's default formatting is usually perfect, so just use that. TIP: select all of the code and click ctrl+i.
Marcus Silverberg
2016년 9월 6일
채택된 답변
추가 답변 (1개)
Mischa Kim
2016년 9월 6일
Marcus, add a
hold on
after the plot command.
댓글 수: 2
Marcus Silverberg
2016년 9월 6일
Mischa Kim
2016년 9월 6일
Not quite sure, what you are trying to achieve. If it is only one plot per subplot, why not use indexing?
c = [10,20,40,80];
for n = 1:4
subplot(1,4,n)
x = linspace(0,2*pi,c(n));
y = atan(x);
plot(y,'*')
end
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
