필터 지우기
필터 지우기

Can I rely on the Handles numeration?

조회 수: 2 (최근 30일)
Nimrodb
Nimrodb 2013년 2월 26일
I have a complex GUI (built with GUIDE) and among others, there are 10 pushbuttons.
Their handles are defined by the GUI. I have noticed that the difference between 1 button handle to the other is 1.
Can I relay on that? I re-ran the GUI several times and the handle values change but the delta remains.
I want to set the buttons 'Value' field in a loop or something like that.
Something like that:
first_h = handles.Button1
for i=1:10
Value = get(first_h + i ,'Value)
end

채택된 답변

Jan
Jan 2013년 2월 26일
편집: Jan 2013년 2월 26일
No, you definitely cannot rely on the value of handles.
For accesing the handles in a loop, simply store them in a vector:
ButtonList = [handles.StatusButton1, handles.StatusButton2, ...
handles.StatusButton3, handles.StatusButton4];
for iButton = 1:length(ButtonList)
set(ButtonList(iButton), 'Value', rand>0);
end
As usual you can see, that hiding an index in the name of a variable is not handy. It would be much smarter, if GUIDE stores the handles in a vector directly, but this is not implemented yet.
Of course meaningful tags could help also, but it would be slower:
for iButton = 1:10
tag = sprintf('Button%d', iButton);
handle = findobj(get(handles.figure, 'children'), 'flat', ...
'Style', 'PushButton', 'tag', tag);
set(handle, 'Value', rand>0);
end

추가 답변 (2개)

Daniel Shub
Daniel Shub 2013년 2월 26일
I wouldn't rely on the handle ids behaving in any sensible way unless you set them at the outset. There is no reason to have to rely on them either. You should either set the handles at the outset and save them in a reasonable place within the appdata or add meaningful tags that allow you to then find the buttons at a later time.
  댓글 수: 2
Nimrodb
Nimrodb 2013년 2월 26일
The tags are ok but I want to have some-sort of a loop to access the button.
Maybe you could help find a different algo... This is what I have:
(I have 10 buttons I added to my GUI and as the simulation progress - I turn the next one 'on')
ProgressBarVec = gSimulationTime/10*[0.001:10]; %gSimulationTime is the simulation length
PrevProgress = 0;
FirstButton = handles.StatusButton1 %%%%%this is what I kind of counted on
while gCurrentTime<gSimulationTime
ButtonIndication = ceil((gCurrentTime - ProgressBarVec)/gSimulationTime);
CurrProgress = max(find(ButtonIndication));
if (CurrProgress > PrevProgress)
set(FirstButton+(CurrProgress-1),'Value',1); %%%%Here the problem will occur if the handles are not kept in const delta
PrevProgress = CurrProgress;
end
{continue of simulation.......}
end %gCurrentTime<gSimulationTime
In the case above, having a meaningful Tag names will not help me.
Walter Roberson
Walter Roberson 2013년 2월 26일
thishandle = handles.(sprintf('StatusButton%d', CurrProgress));

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2013년 2월 26일
No, I don't think you can rely on that. In particular, most object handles are actually floating point numbers, not integers, and you should use the full precision to access them. (See, for example, this page: http://www.mathworks.com/help/matlab/creating_plots/accessing-object-handles.html.)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by