フィルターのクリア

use of eval

2 ビュー (過去 30 日間)
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 10 月 21 日
I am trying to use evalfunction but are having a hard time:
>> plots=[3 4 6 1]
plots =
3 4 6 1
>> whatplot=input('type type of graph: ','s')
type type of graph: bar
whatplot =
bar
>> eval(whatplot(plots))
??? Index exceeds matrix dimensions.
>>
why wont it evaluate it to a graph with bars?

採用された回答

Wayne King
Wayne King 2011 年 10 月 21 日
Your call to input() returns the string bar in whatplot
but whatplot(plots) is not a string.
eval([whatplot '(plots)'])
  4 件のコメント
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 10 月 21 日
thanks! I guess I am also a bit confussed about my book. It says:
>>x=[9 7 10 9];
whatplot=input('What type of plot?: ','s');
What type of plot?: bar
>>eval([whatplot '(x)'])
>>title(whatplot)
>>xlabel('Student#')
>>ylabel('Quiz Grade')
I guess the three last lines are just for naming graph and coordinates but thoose before don't return any graph for me
Jan
Jan 2011 年 10 月 22 日
Bad book. Evil EVAL.

サインインしてコメントする。

その他の回答 (2 件)

Patrick Kalita
Patrick Kalita 2011 年 10 月 21 日
I think feval would be a better option here. It is a safer than eval, and it easily accommodates passing input arguments:
feval( whatplot, plots )
  3 件のコメント
Jan
Jan 2011 年 10 月 21 日
@Tor: Avoid EVAL. As you see, it causes troubles.
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 10 月 21 日
it is part of curriculum unfortunately:) If not I would have:)

サインインしてコメントする。


Wayne King
Wayne King 2011 年 10 月 21 日
plots=[3 4 6 1];
whatplot = 'bar';
eval([whatplot '(plots)'])
  1 件のコメント
Wayne King
Wayne King 2011 年 10 月 21 日
also, see the comments by other posters above.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by