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
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
Daniel Liberman 2020 年 3 月 14 日
Thank You,
Now Im having another problem.
I used asingin function to see what is the input I get for Xn, Yn, Dn.
My inputs are: for Xn-0,1.5,0,5,6.5,10 , for Yn-0,0,1.5,4,4,6 , for dn-1,1,1,1,1,1, but what I get is:
% --- 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=str2double(get(handles.edit1,'String'));
assignin('base','Xn',Xn);
Yn=str2double(get(handles.edit2,'String'));
assignin('base','Yn',Yn);
dn=str2double(get(handles.edit3,'String'));
assignin('base','dn',dn);
Geoff Hayes
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
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
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
Daniel Liberman 2020 年 3 月 18 日
Geoff Hayes, I tryed using str2num and it returns me the following error: "Error using str2num (line 31)
Requires character vector or array input."
Adam Danz, the input is something the user should type to the edit texts in the GUI, not directly to the code.
Adam Danz
Adam Danz 2020 年 3 月 18 日
編集済み: Adam Danz 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
Daniel Liberman 2020 年 3 月 18 日
Now I'm getting the following error:
"Error using strsplit (line 80)
First input must be either a character vector or a string scalar."
Adam Danz
Adam Danz 2020 年 3 月 18 日
編集済み: Adam Danz 2020 年 3 月 18 日
Please show us the input.
Daniel Liberman
Daniel Liberman 2020 年 3 月 18 日
thit is how the input looks like
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 日

1 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by