필터 지우기
필터 지우기

how can I do it without using eval

조회 수: 2 (최근 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 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