use of eval

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

Accepted Answer

Wayne King
Wayne King on 21 Oct 2011
Your call to input() returns the string bar in whatplot
but whatplot(plots) is not a string.
eval([whatplot '(plots)'])
  4 Comments
Jan
Jan on 22 Oct 2011
Bad book. Evil EVAL.

Sign in to comment.

More Answers (2)

Patrick Kalita
Patrick Kalita on 21 Oct 2011
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 Comments
Tor Fredrik Hove
Tor Fredrik Hove on 21 Oct 2011
it is part of curriculum unfortunately:) If not I would have:)

Sign in to comment.


Wayne King
Wayne King on 21 Oct 2011
plots=[3 4 6 1];
whatplot = 'bar';
eval([whatplot '(plots)'])
  1 Comment
Wayne King
Wayne King on 21 Oct 2011
also, see the comments by other posters above.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by