Using the Plot function with a text string
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 22 Set 2015
Modificato: Stephen23
il 22 Set 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
Risposta accettata
Thorsten
il 21 Set 2015
Modificato: Thorsten
il 21 Set 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 Commenti
Stephen23
il 21 Set 2015
"You do not even need eval"
Don't use eval, it is a slow and non-robust solution for such trivial code.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Title in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!