Using the Plot function with a text string

5 views (last 30 days)
I've been searching for ages but i can't seem to work this out.
I am making a GUI plotting function that changes based on how many sensors are being used
ie: plot(handles.s,handles.Data_Input(:,00001),handles.s,handles.Data_Input(:,00002))
When I write the function out manually it works fine, but to automate it I have written the following code. Though this turns it into a string and then the plot function doesn't seem to read it correctly. ...
b=1;
i=2;
for b=1:i
a = sprintf('%s%s%s%05i%s','handles.s',',','handles.Data_Input(:,',b,')');
c(b,:) = a;
b=b+1;
end
e=(strjoin(cellstr(c).',','));
plot(e)
so "e" prints like this "handles.s,handles.Data_Input(:,00001),handles.s,handles.Data_Input(:,00002)" which if I typed in manually would plot correctly.
however when I use the plot(e) function I get the error, "Error using plot Invalid first data argument."
If anyone has any ideas on how to fix this or another way to make this plot function that can change with different number of sensors that would be great.
  3 Comments
Chris Pevitt
Chris Pevitt on 22 Sep 2015
Hey, yeah I am aware that it's not the best way to code it, and I have other ways to do the main part, however I still end up with a string as my result and I can't plot a string.
I'm not sure how to do it in a way where they are read as a variable.
My main issue is that the sting looks correct. Such that if you manually copied the text and paste it in the plot function it will work correctly, as it will read them as variable. But when I run it through the code it reads it as a string and I get the error "Invalid first data argument."
Any ideas of how to get around this or a method to make a similar sting but as variable?
Normally I am happy to read everything and work it out myself but this time I just can't work it out
Stephen23
Stephen23 on 22 Sep 2015
Edited: Stephen23 on 22 Sep 2015
Do you see the connection?: "it's not the best way to code it," and "the sting looks correct... But when I run it... I get the error "Invalid first data argument."" Using eval is such a bad way to code that it makes debugging almost impossible. It isn't even possible to know where the error occurs: as you state "it looks correct" is the best that one can do, but this is not really a very robust measure of code correctness.
If this was coded properly, without eval, then MATLAB has lots of useful tool to help track down bugs. Using eval removes all of those useful tools: code hinting, variable highlighting, tab completion, mlint messages and many other useful tools from the editor. Using eval makes the code obfuscated and unclear and difficult to bug fix, because it can only be analyzed at run time. So by using eval beginners are making their own lives more difficult, and then get stuck thinking "but this is the only way to solve this problem..." without realizing that their "solution" is actually causing them more problems than it solves.
Avoid eval

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 21 Sep 2015
Edited: Thorsten on 21 Sep 2015
eval(['plot(' e ')']
Note that you could get the whole work done in two lines
e = sprintf('handles.s,handles.Data_Input(:,%05d),', [1 2]);
eval(['plot(' e(1:end-1) ')'] % end-1 to get rid of the trailing ','
You do not even need eval
axis; hold on
for ind = [1 2]
plot(handles.s,handles.Data_Input(:,ind))
end
  2 Comments
Stephen23
Stephen23 on 21 Sep 2015
"You do not even need eval"
Don't use eval, it is a slow and non-robust solution for such trivial code.
Chris Pevitt
Chris Pevitt on 22 Sep 2015
Ahh got so caught up in the way I was doing it, didn't even consider such and easy way
thanks heaps, solved everything

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!