Borrar filtros
Borrar filtros

Vector Input, GUI edit text box

10 visualizaciones (últimos 30 días)
Daniel Liberman
Daniel Liberman el 13 de Mzo. de 2020
Comentada: Adam Danz el 18 de Mzo. de 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 comentarios
Daniel Liberman
Daniel Liberman el 18 de Mzo. de 2020
this*
Daniel Liberman
Daniel Liberman el 18 de Mzo. de 2020
No brackets, just commas

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 18 de Mzo. de 2020
Editada: Adam Danz el 18 de Mzo. de 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 comentarios
Daniel Liberman
Daniel Liberman el 18 de Mzo. de 2020
Thank you, It works :)
Adam Danz
Adam Danz el 18 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by