The edit field must have a mathematical equation alert using MATLAB GUIDE
3 views (last 30 days)
Show older comments
I want to add a validation to the first edit field shown in the following interface:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319195/image.png)
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
0 Comments
Accepted Answer
Joost
on 20 Jun 2020
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 Comments
Joost
on 22 Jun 2020
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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!