How can I convert a string to a function input?

3 次查看(过去 30 天)
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.

回答(2 个)

Thorsten
Thorsten 2015-11-25
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 2015-11-25
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);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by