필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
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 CenterFile Exchange에서 Loops 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