Borrar filtros
Borrar filtros

How to solve an operation written on a EditField?

2 visualizaciones (últimos 30 días)
I am new to Matlab, I have been using it to do some math. I created a very easy program with App Designer, the idea is that the user can enter almost any expression and then display the result, for example "7 + 4" or "4x + 2 = 0". The problem is, I don't know how to "convert" the text from the EditField to a mathematical expression. I did a little test with "2 + 4" using the "str2sym ()" function and the "num2str ()" function, but when setting the result in the other EditField, it shows me this error: Error using num2str (line 47) Input to num2str must be numeric.
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
expr = str2sym(app.ExpressionEditField.Value);
app.ResultEditField.Value = num2str(expr);
end
end
I don't know what to do, any recommendation is appreciated.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Ag. de 2021
The result of str2sym() is symbolic. num2str() cannot convert symbolic to string.
Instead of using num2str(expr) you can use string(expr)
Note: the expression to be converted must be valid MATLAB syntax. "4x + 2 = 0" is not valid MATLAB syntax. The user would have had to have entered "4*x + 2 == 0", or else you would have to have processed what the user did enter in order to make it into valid MATLAB syntax.
MATLAB does not have any implied multiplication -- no "4x" as meaning to multiply x by 4. Not anywhere that I can find.
  3 comentarios
Walter Roberson
Walter Roberson el 22 de Ag. de 2021
Use subs(), like
syms x
y = 4 * x + 2
y = 
subs(y, x, 2)
ans = 
10
Daniel Martínez
Daniel Martínez el 22 de Ag. de 2021
Thanks, it's exactly what I was looking for.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by