Plot many curves in one plot using for loop

조회 수: 3 (최근 30일)
Benjamin Cowen
Benjamin Cowen 2017년 7월 17일
답변: Robert U 2017년 7월 18일
Is there a way to have the variables in the plot change?
I am trying to do this:
for i=1:1:8
plot(x(i),o_vac(i))
hold on
end
But I get a grey screen for my figure. I want to plot 8 graphs with x1, y1, then x2, y2. Is this possible?
  댓글 수: 2
Stephen23
Stephen23 2017년 7월 17일
편집: Stephen23 2017년 7월 17일
plot(x(i),o_vac(i))
does not make much sense: you are plotting single points in a loop. This is very inefficient: if that is what you want then just plot all points at once and select the linestyle to suit.
Why do you need to use a loop? Just put your data into a matrix (arranged by column) and one single plot call is all you need:
plot(X,Y)
Benjamin Cowen
Benjamin Cowen 2017년 7월 17일
They are not suppose to be single points. I read in the data as shown below. I just have many of these, and don't want to write it all out
data1 = xlsread('C:\Users\Ben\examples.xlsx','PKA1');
x1 = data1(:,1);
o_vac1 = data1(:,4);

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

답변 (1개)

Robert U
Robert U 2017년 7월 18일
Hi Benjamin,
the following code snippet works as you would expect it to do:
% create data
for ik = 1:4
x(:,ik) = (-2:0.01:2)*pi/4*ik;
A(:,ik) = sin(x(:,ik) + ik*pi/4);
end
% plot data
fh = figure;
ah = axes('Parent',fh);
hold on
for ik = 1:4
plot(ah,x(:,ik),A(:,ik))
end
My suggestion would be to check on data validity. Are all values numeric? Are there blank spaces (empty cells) or nan-values?
Kind regards,
Robert

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by