Using the Plot function with a text string
이전 댓글 표시
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
Ugh, don't use eval for trivial code like this! Learn to program faster and more robust code by not using eval.
Chris Pevitt
2015년 9월 22일
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
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!