MATLAB: How can I do it without using eval

evalhandles

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

Best Answer

  • 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