Filter löschen
Filter löschen

how can I do it without using eval

1 Ansicht (letzte 30 Tage)
G A
G A am 17 Jun. 2019
Bearbeitet: G A am 18 Jun. 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 Kommentare
Walter Roberson
Walter Roberson am 17 Jun. 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 am 18 Jun. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 17 Jun. 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 Kommentar
G A
G A am 18 Jun. 2019
Bearbeitet: G A am 18 Jun. 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.:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by