How can I convert a string to a function input?

3 Ansichten (letzte 30 Tage)
Simon Kuster
Simon Kuster am 25 Nov. 2015
Beantwortet: Simon Kuster am 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.

Antworten (2)

Thorsten
Thorsten am 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 am 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);

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by