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일

0 개 추천

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일
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 2011년 10월 21일
eval([whatplot, '(plots)'])
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일

4 개 추천

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

Fangjun Jiang
Fangjun Jiang 2011년 10월 21일
Yes. feval() is the way to go. I was thinking about inline(), @, etc.
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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by