How can I convert a string to a function input?

5 views (last 30 days)
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.

Answers (2)

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

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!