필터 지우기
필터 지우기

Vector Input, GUI edit text box

조회 수: 10 (최근 30일)
Daniel Liberman
Daniel Liberman 2020년 3월 13일
댓글: Adam Danz 2020년 3월 18일
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
Daniel Liberman
Daniel Liberman 2020년 3월 18일
this*
Daniel Liberman
Daniel Liberman 2020년 3월 18일
No brackets, just commas

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 3월 18일
편집: Adam Danz 2020년 3월 18일
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
Daniel Liberman
Daniel Liberman 2020년 3월 18일
Thank you, It works :)
Adam Danz
Adam Danz 2020년 3월 18일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by