Evaluate user input expression
Mostrar comentarios más antiguos
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
Respuestas (2)
Walter Roberson
el 15 de En. de 2017
1 voto
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
el 14 de En. de 2017
>> 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 comentarios
Rupamathi Jaddivada
el 15 de En. de 2017
Image Analyst
el 15 de En. de 2017
MATLAB will do that for you.
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!