how can I do it without using eval

1 回表示 (過去 30 日間)
G A
G A 2019 年 6 月 17 日
編集済み: G A 2019 年 6 月 18 日
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by