Vector Input, GUI edit text box
Show older comments
Hi,
I am trying to get a vector input from the user in a GUI using edit text boxe, but it seems that the program doesn't recognize the text boxes, although I have them in my GUI. Can someone tell what is the problem?
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Xn=str2num(get(handle.edit1,'string'));
Yn=str2num(get(handle.edit2,'string'));
dn=str2num(get(handle.edit3,'String'));
The class handle has no Constant property or Static method named 'edit1'.
12 Comments
Sriram Tadavarty
on 14 Mar 2020
Hi Daniel,
Replace the variable 'handle' with 'handles' while calculating Xn, Yn and Dn. This should solve.
Regards,
Sriram
Daniel Liberman
on 14 Mar 2020
Geoff Hayes
on 16 Mar 2020
Daniel - is there a reason that you are assigning these varaibles to the base workspace? Why not just add them as fields to the handles structure so that other callbacks in your app can access them?
Geoff Hayes
on 16 Mar 2020
Also, try using str2num instead of str2double since the former should preserve the array:
>> x = '-0,1.5,0,5,6.5,10';
>> str2num(x)
ans =
0 1.5000 0 5.0000 6.5000 10.0000
>> str2double(x)
ans =
NaN
Adam Danz
on 16 Mar 2020
Another way to convert the values without using str2num (which uses eval and can lead to undexpected behaviors) is,
x = '-0,1.5,0,5,6.5,10';
d = str2double(strsplit(x, ','))
or, depending on how the values are delimited,
x = '-0 1.5 0 5 6.5 10';
d = str2double(strsplit(x))
Daniel Liberman
on 18 Mar 2020
"the input is something the user should type to the edit texts in the GUI, not directly to the code."
Yeah, I'm aware of that. The x values in my comment were for demonstration purposes. They should be similar to the values you get from
x = handle.edit1.string;
% or
x = get(handle.edit1,'string')
If you get an error, we'll need to see what the input looks like.
Note that this method allows the user to enter anything including typos, brackets, and other symbols that won't be interpretted correctly. You could use a try/catch to prompt the user to try again if they didn't enter a proper input.
Daniel Liberman
on 18 Mar 2020
Daniel Liberman
on 18 Mar 2020
Daniel Liberman
on 18 Mar 2020
Daniel Liberman
on 18 Mar 2020
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings 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!
