How can I convert a string to a function input?

3 vues (au cours des 30 derniers jours)
Simon Kuster
Simon Kuster le 25 Nov 2015
I'd like to convert a string so, that it seems to be an list of arrguments.
x = 1:1:10;
y = 2:2:20;
str = 'x,y';
plot = (function(str)); %should work like: plot(x,y)
I try to do it with the command eval, but I didnt get it work.
x = 1:1:10;
y = 2:2:20;
str = 'x,y';
input = eval(str);
plot(input);
Has someone an idee, how I could solve this problem? Thanks.

Réponses (2)

Thorsten
Thorsten le 25 Nov 2015
You can do this
eval(['plot(' str ')'])
but why do you want to? Using eval often leads to messy code, and there are better solutions. But to give some advice, please tell us what you want to do.

Simon Kuster
Simon Kuster le 25 Nov 2015
Thanks for your fast responce. Your solution works perfect.
I wanted to create an input, to plot several lines in one command.
I colleague told me to use 'hold on', so I can use now the normal input for a plot.
figure(1)
plot(x,y);
hold on;
plot(x1,y1);
plot(x2,y2);

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by