フィルターのクリア

How to solve an operation written on a EditField?

2 ビュー (過去 30 日間)
Daniel Martínez
Daniel Martínez 2021 年 8 月 22 日
コメント済み: Daniel Martínez 2021 年 8 月 22 日
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.

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 22 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 8 月 22 日
Use subs(), like
syms x
y = 4 * x + 2
y = 
subs(y, x, 2)
ans = 
10
Daniel Martínez
Daniel Martínez 2021 年 8 月 22 日
Thanks, it's exactly what I was looking for.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by