The edit field must have a mathematical equation alert using MATLAB GUIDE

2 ビュー (過去 30 日間)
T.Mariah Khayat
T.Mariah Khayat 2020 年 6 月 19 日
コメント済み: Joost 2020 年 6 月 22 日
I want to add a validation to the first edit field shown in the following interface:
So, if the user enters something rather than a mathematical equation, an error message must appear to the user.
How can I do such a validation, what shall I be writing exactly in the edit field call back function to do that?
function editEquation_Callback(hObject, eventdata, handles)
% hObject handle to editEquation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editEquation as text
% str2double(get(hObject,'String')) returns contents of editEquation as a double
if ()
errordlg('Enter a valid mathematical equation', 'Error', 'modal');
else
%do nothing
end

採用された回答

Joost
Joost 2020 年 6 月 20 日
I think you can use the str2func command for this.
There is a nice example on the documentation page.
First put @(x) in front of the input field text and then use str2func to create an anonymous function.
This function can then be used in with feval to evaluate. And to detect for errors etc you can put a try-catch construct around the whole.
Good luck!
ps you might want to have a look at App Designer for creating such user interfaces. GUIDE is superseded by it. App Designer works great and intuitively. There is an conversion tool available for upgrading GUIDE guis to App Designer apps.
  5 件のコメント
Joost
Joost 2020 年 6 月 20 日
will do coming Monday. Good steps already.
Joost
Joost 2020 年 6 月 22 日
Hello,
I think this solves your problem:
% func = hObject.String;
func = 'x-x^(1/3)-2';
% func = 'bla';
% func = '[x x]';
% func = 'char(x)';
try
f = str2func(['@(x) ', func]);
% Try the function if it can be evaluated. We just use dummy input x=1.
y = f(1);
assert(isscalar(y), 'BiSect:NonScalar', 'Function should produce scalar output.');
assert(isnumeric(y), 'BiSect:NotNumeric', 'Function should produce numeric output.');
% Add your own validation tests here.
% If the code execution reaches this point, everything should be fine.
catch ME
errordlg(['Enter a valid mathematical equation. ' ME.message], 'Error', 'modal');
end
The first line is the retrieval of the user input. I put several other func= lines there to test my script. These can of course be removed once you are satisfied with the script.
You do not need the syms x line.
I added validation tests for numeric and scalar output. You can add your own tests there if more or other checks apply.
The errordlg now also shows what is wrong with the input. That is helpful for the user.
Finally I feel obliged to give a warning on the use of eval / feval / str2func. A user with wrong intentions could potentially do harm to your system by filling in commands in the text edit field that affect your system. That makes the system potentially vulnerable for attacks. So please take this into account when distributing the app. Maybe it needs a lot more validation to prevent such harmful input strings being executed.
Good luck!
Best regards, Joost

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by