필터 지우기
필터 지우기

Select variables for plot

조회 수: 40 (최근 30일)
Jose
Jose 2012년 3월 31일
Hello
I have a vector and a matrix obtained from another function, but the structure is like the vector
and matrix inside 'callplot'.
The code, is just like this. I really dont know how to write in the right way this
pl(1:2)='t1,y1(:,1)'
inside the if function.
function f=callplot
t1=[0;1;2;3;4;5];
y1=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20; 21 22 23 24];
in1=0; in2=0; in3=0; in4=0;
a=0; b=0; c=0;
while in1~='Y'||'N';
in1=input('¿You want to plot the variable X? (Y/N)','s');
if in1=='Y' pl(1:2)='t1,y1(:,1)'; elseif in1=='N' a=1; end
end
while in2~='Y'||'N';
in2=input('¿You want to plot the variable R? (Y/N)','s');
if in2=='Y' pl(3-2*a:4-2*a)='t1,y1(:,2)'; elseif in2=='N' b=1; end
end
while in3~='Y'||'N';
in3=input('¿You want to plot the variable N? (Y/N)','s');
if in3=='Y' pl(5-2*(a+b):6-2*(a+b))='t1,y1(:,2)'; elseif in3=='N' c=1; end
end
while in4~='Y'||'N';
in4=input('¿You want to plot the variable Z? (Y/N)','s');
if in4=='Y' pl(7-2*(a+b+c):8-2*(a+b+c))='t1,y1(:,2)'; end
end
plot(pl)
%If all variables are selected to be plotted
%It should do the same as this
%plot(t1,y1(:,1),t1,y1(:,2),t1,y1(:,3),t1,y1(:,4))
%but obviously, I want pl can have any structure posible
%according to the user plot choices.
end
The error displayed when I Input Y is:
??? Subscripted assignment dimension mismatch.
Error in ==> callplot at 9 if in1=='Y' pl(1:2)='t1,y1(:,1)'; elseif in1=='N' a=1; end
Additionally, it looks like the elseif isn't working either. When I Input N, it just display the
prompt again (It should only happen when input is different from Y or N). I suspect my error is on the ~='Y'||'N'
I hope to be clear enough, sorry if my english is not good.

채택된 답변

Aaditya Kalsi
Aaditya Kalsi 2012년 4월 1일
The short answer is writing code like this is bad practice when it comes to changing it later and may bring many other problems. The other short answer is EVAL. You can use something like:
eval(['plot(' pl ');']);
The better way to do this:
% if you must use input
answersGiven = {in1; in2; in3; in4; in5};
% gets the columns to be selected
boolCols = strcmpi('y', answersGiven);
% converts logical into column numbers
colNums = find(boolCols == true);
% plot(t1, y1(:, colNums));
This should do it. I didn't have MATLAB open so the code may gve errors! Sorry about that. But the idea should be pretty clear.
  댓글 수: 1
Jose
Jose 2012년 4월 1일
I like your way, I'll try that the next time.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 4월 1일
while in1~='Y'||'N';
is interpreted as
while (in1~='Y') || 'N';
which is
while (in1~='Y') || ('N' ~= 0)
Now as 'N' is not 0, the statement is always true.
You want an "and" instead of an "or"
while in1~='Y' && in1 ~='N';
What do you want
pl(1:2)='t1,y1(:,1)'
to mean?
  댓글 수: 1
Jose
Jose 2012년 4월 1일
Thx, i really figured out how to do it yesterday, just like you said.
About the pl(1:2)='t1,y1(:,1)', I found the right way to do it, it was
plt=t1; ply(:,1)=y1(:,1)
So when i get all the columns selected for ply, I simply use plot(plt,ply).

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

카테고리

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