How can I convert a string to a function input?

3 ビュー (過去 30 日間)
Simon Kuster
Simon Kuster 2015 年 11 月 25 日
回答済み: Simon Kuster 2015 年 11 月 25 日
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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by