Hi, I was wondering if there is a way to evaluate complicated user input expression If 'a' and 'b' are vectors and if user input is 'a*b' or 'a*b^2', how do I evaluate the expression?
Thanks, Rupa

답변 (2개)

Walter Roberson
Walter Roberson 2017년 1월 15일

1 개 추천

If you read the expression as a string, then you can use symvar() to pull out the names of the variables the user used. You can then use exist() to test that each exists. Once you have done that, you can use vectorize() to convert * and / to .* and ./ . From there you could str2func and call that function passing in the values for the variables. Or you could eval()
dpb
dpb 2017년 1월 14일

0 개 추천

>> a=1:3;
>> v=input('Enter Matlab vector expression for a: ')
Enter Matlab vector expression for a: a.^2
v =
1 4 9
>>
Of course, this can be very dangerous...

댓글 수: 2

Rupamathi Jaddivada
Rupamathi Jaddivada 2017년 1월 15일
Is there a way I can check each term of the expression is defined in the code or not. For example, if user enters a.*b.*c and if b is not defined, can I display an error saying that 'b is not defined'?
Image Analyst
Image Analyst 2017년 1월 15일
MATLAB will do that for you.

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

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

질문:

2017년 1월 14일

답변:

2017년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by