how can I do it without using eval

2 次查看(过去 30 天)
There are quite a few handles of uicontrols and uipanels named h1,h2...hN in my code exported by GUIDE. I want to create structure of handles with names handles.(Tag) for all uicontrols. How can I do it without using eval?
for k=2:N
ns=num2str(k);
hs=eval(['h',ns]);
Tag=get(hs,'Tag');
handles.(Tag)=hs;
end
  3 个评论
Walter Roberson
Walter Roberson 2019-6-17
If it is code exported by GUIDE, then GUIDE will automatically create those handles for you. It is done as part of the initialization of the gui. It goes something like
handles_with_tags = findobj(GUI, '-property', 'Tag');
for K = 1 : length(handles_with_tags)
this_handle = handles_with_tags(K);
thistag = get(this_handle, 'Tag');
if isvarname(thistag)
handles.(thistag) = this_handle;
end
end
Except that it does extra work so that when it finds multiple objects with the same tag, it creates a vector of handles.
G A
G A 2019-6-18
Thanks, Walter!
That is what I want. I was just modyfiing (mainly in learning purpose) my code from the form of GUIDE export to the form decribed in the tutorial:https://uk.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-6-17
Assuming the handles are stored in a vector,
allhand = [h1,h2,h3]; %row vector
tags = get(allhand, 'tag');
handles = cell2struct(num2cell(allhand)',tags); %no need for transpose if allhand is column vector
  1 个评论
G A
G A 2019-6-18
编辑:G A 2019-6-18
The problem is that the handles are not stored in an array or may be stored manually. How to store them not manually without using eval - that was my question. Newertheless I accept your answer because I am satisfied by Walter's answer and I have learned something from yours.:)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by