Azzera filtri
Azzera filtri

Vector Input, GUI edit text box

10 visualizzazioni (ultimi 30 giorni)
Daniel Liberman
Daniel Liberman il 13 Mar 2020
Commentato: Adam Danz il 18 Mar 2020
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 Commenti
Daniel Liberman
Daniel Liberman il 18 Mar 2020
No brackets, just commas

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 18 Mar 2020
Modificato: Adam Danz il 18 Mar 2020
The string from a edit box is returned as a cell array of characters. If the expected inputs are a comma separated vector such as "1, 2, 3.14, 5", here's how to exact those values.
s = handles.edit1.String;
d = str2double(strsplit(s{:}, ','));
I suggest using conditional error detection in order to provide the user with feedback in case they use an incompatible format.
s = handles.edit1.String;
try
d = str2double(strsplit(s{:}, ','));
catch
error('Edit field must contain comma separated values such as "6, 5, 3.14"')
end
  2 Commenti
Daniel Liberman
Daniel Liberman il 18 Mar 2020
Thank you, It works :)
Adam Danz
Adam Danz il 18 Mar 2020
The string value extracted from the edit box is actually a cell array of characters. So, if the user enters "1,1,2,4" the string output will be {'1,1,2,4'}. The {:} part of my answer solves that by returning the character array within the cell array.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by