Vector Input, GUI edit text box
이전 댓글 표시
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
Sriram Tadavarty
2020년 3월 14일
Hi Daniel,
Replace the variable 'handle' with 'handles' while calculating Xn, Yn and Dn. This should solve.
Regards,
Sriram
Daniel Liberman
2020년 3월 14일
Geoff Hayes
2020년 3월 16일
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
2020년 3월 16일
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
2020년 3월 16일
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
2020년 3월 18일
"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
2020년 3월 18일
Daniel Liberman
2020년 3월 18일
Daniel Liberman
2020년 3월 18일
Daniel Liberman
2020년 3월 18일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

