Using the Plot function with a text string
조회 수: 8 (최근 30일)
이전 댓글 표시
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
Stephen23
2015년 9월 22일
편집: Stephen23
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
채택된 답변
Thorsten
2015년 9월 21일
편집: Thorsten
2015년 9월 21일
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
Stephen23
2015년 9월 21일
"You do not even need eval"
Don't use eval, it is a slow and non-robust solution for such trivial code.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!