The program doesn't re-read variables's values in Matlab app.designer

3 views (last 30 days)
I made the following program in matlab app.designer.
The point here is that you introduce the variables "m" and "a" in whatever editfields on the left and their numeric values in whatever fields on the right.
Then, by clicking on CALCULATE button it appears the value of the Force (F=m*a) in the centre as you can see in the image
Then, you click on REFRESH button and the numeric values return to 0 and the text values return to empty.
Everything goes fine right to that point.
The issue here is that after clicking on REFRESH, and I want to introduce again variables and values...the program no longer reads the variables and their values, so the program doesn't calculate the value of the force of course. Just it doesn't do anything and appears that error on the command window. (By the way, app's code goes just until line 155, I don't know what that line 335 means.)
The code I have used for CALCULATE button and REFRESH button are those:
Thank you very much.

Answers (1)

Rik on 13 Jan 2021
You should probably consider stripping all whitespace from the user input.
This looks to me like the value in the text field is not equal to either of your branches, meaning m is not defined. You are also using == to compare char arrays, instead of strcmp (which would also handle strings and will handle arrays of dissimilar size).
switch app.text1.Value
case 'm'
case 'a'
%make sure your user gets an error here, or set default values
Another possibility is to use a dropdown menu or radio buttons to allow the user to select variables.
Rik on 13 Jan 2021
You assume both time that the user meant to write 'a'. If you have ' a' and ' m', both branches will define a, leaving m undefined. You also made a copy-paste error: in the second case you probably mean val1 instead of val2.
Since you only post image, instead of a code file, I can't see what your refresh button is doing, nor what you attempted to do with the dropdown. The dropdown (or radio button) will work if properly implemented and will make sure your user can't mess it up by writing 'ma' in one of the fields.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!