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!

