Select variables for plot

61 views (last 30 days)
Jose
Jose on 31 Mar 2012
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.

Accepted Answer

Aaditya Kalsi
Aaditya Kalsi on 1 Apr 2012
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 Comment
Jose
Jose on 1 Apr 2012
I like your way, I'll try that the next time.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 1 Apr 2012
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 Comment
Jose
Jose on 1 Apr 2012
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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!