ERORR "CANNOT FIND 'GET' METHOD FOR STRING CLASS.

9 次查看(过去 30 天)
I'm coding with GUIDE, i want to get data input from handles.edit1,...handles.edit8 and save to variable handles.in(1,1),...handles.in(1,8). My code is below and I get an error "Cannot find 'get' method for string class.":
edit = ["handles.edit1","handles.edit2","handles.edit3","hanles.edit4","handles.edit5","handles.edit6","handles.edit7","handles.edit8"]
for i = 1:1:8
handles.in(1,i) = str2num(get(edit(1,i),'string'));
end

回答(2 个)

Geoff Hayes
Geoff Hayes 2020-4-7
Khanh - the problem is that edit is an array of strings to edit text control handles and is not an array of the handles themselves. So the error message makes sense. If you want to create an array of the handles then just do
editHandles = [handles.edit1, handles.edit2, handles.edit3, handles.edit4, handles.edit5, handles.edit6, handles.edit7, handles.edit8];
Alternatively, if you are interested in only those handles that are named edit*, then you could replace the above with
editHandles = findobj(handles.figure1, '-regexp', 'Tag', 'edit[0-9]');
where we find all the (child) objects of (parent) of the GUI (handles.figure1) whose Tag matches the expression edit with a numeric suffix. The order is possibly with the last added edit control first, so you may need to do some additional sorting or just be aware that the order of the edit text controls isn't handles.edit1, handles.edit2, etc.
  1 个评论
Khanh Trinh
Khanh Trinh 2020-4-9
Thank you so much, I did it. Sorry if I talked impolite because I was poor in English

请先登录,再进行评论。


Steven Lord
Steven Lord 2020-4-7
The approaches Geoff Hayes gave are two ways you can solve this problem. Two others:
Because handles is a struct array, you could use dynamic field names or getfield to retrieve fields from it whose names are given as text.
s = 'edit7';
A = handles.(s).String; % The String from handles.edit7
h7 = getfield(handles, s);
B = h7.String;
With the capability to dot index into function calls introduced in release R2019b, you could potentially combine those last two lines but I left them separate for clarity.
Another alternative, and one that I recommend adopting, is to change the names of your edit boxes from the defaults (edit1 through edit8) to something more indicative of their purpose. You would lose the capability to iterate through the boxes by building the Tags from "edit" followed by a number, but then again your edit boxes likely aren't interchangeable. Giving them each a Tag that states their purpose may make your code easier to understand. Which of the following could you explain the purpose of better just based on the line of code alone?
A = handles.edit4.String;
B = handles.PatientName.String;
  1 个评论
Khanh Trinh
Khanh Trinh 2020-4-9
Thank you so much, I did it. Sorry if I talked impolite because I was poor in English

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by