use of eval

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?

 Respuesta aceptada

Wayne King
Wayne King el 21 de Oct. de 2011

0 votos

Your call to input() returns the string bar in whatplot
but whatplot(plots) is not a string.
eval([whatplot '(plots)'])

4 comentarios

Tor Fredrik Hove
Tor Fredrik Hove el 21 de Oct. de 2011
I cant seem to get this right:
plots=[3 4 6 1]
plots =
3 4 6 1
>> whatplot='bar'
whatplot =
bar
>> eval([whatplot'(plots)'])
??? eval([whatplot'(plots)'])
|
Error: Unbalanced or unexpected parenthesis or bracket.
>>
Andrei Bobrov
Andrei Bobrov el 21 de Oct. de 2011
eval([whatplot, '(plots)'])
Tor Fredrik Hove
Tor Fredrik Hove el 21 de Oct. de 2011
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 el 22 de Oct. de 2011
Bad book. Evil EVAL.

Iniciar sesión para comentar.

Más respuestas (2)

Patrick Kalita
Patrick Kalita el 21 de Oct. de 2011

4 votos

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 comentarios

Fangjun Jiang
Fangjun Jiang el 21 de Oct. de 2011
Yes. feval() is the way to go. I was thinking about inline(), @, etc.
Jan
Jan el 21 de Oct. de 2011
@Tor: Avoid EVAL. As you see, it causes troubles.
Tor Fredrik Hove
Tor Fredrik Hove el 21 de Oct. de 2011
it is part of curriculum unfortunately:) If not I would have:)

Iniciar sesión para comentar.

Wayne King
Wayne King el 21 de Oct. de 2011

0 votos

plots=[3 4 6 1];
whatplot = 'bar';
eval([whatplot '(plots)'])

1 comentario

Wayne King
Wayne King el 21 de Oct. de 2011
also, see the comments by other posters above.

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by