how can I do it without using eval

1 vue (au cours des 30 derniers jours)
G A
G A le 17 Juin 2019
Modifié(e) : G A le 18 Juin 2019
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 commentaires
Walter Roberson
Walter Roberson le 17 Juin 2019
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 le 18 Juin 2019
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

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 17 Juin 2019
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 commentaire
G A
G A le 18 Juin 2019
Modifié(e) : G A le 18 Juin 2019
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.:)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by