Press button automatically click

조회 수: 5 (최근 30일)
Andre Gomes Bontempo
Andre Gomes Bontempo 2016년 3월 25일
댓글: Walter Roberson 2016년 3월 25일
I'm modifing a code, and I want to use a function that make the code automatically press a button. I searched in mathworks but didn't find anything
for k = 1:3
buttons(k) = uicontrol('units','normal','style','pushbutton', ...
'pos',[.23+.18*k .02 .16 .08],'fontweight','bold', ...
'callback');

답변 (2개)

Walter Roberson
Walter Roberson 2016년 3월 25일
You can retrieve the callback property and invoke the associated function. The way of doing that depends upon whether the property is a string or a function handle or cell array (the first member of which would be a function handle.) You did not show what you were going to populate the callback property with.
Example:
cb = get(buttons(1), 'callback'); %function handle case
cb(buttons(1), []) %invoke it passing the handle of the control
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 3월 25일
For string callbacks, you would usually use
cb = get(buttons(1), 'callback');
evalin('base', cb)
However, your string uses cbo. Fortunately, at least up to R2014a, that can be faked out:
cb = get(buttons(1), 'callback');
fig = ancestor(buttons(1), 'figure');
figure(fig); %need to make it the current figure
set(fig, 'CurrentObject', buttons(1)) %and update gco
evalin('base', cb)
Now, if you need gcbo to also be set, then you are going to have to get into Java Robot class to simulate a click on the button.

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


Andre Gomes Bontempo
Andre Gomes Bontempo 2016년 3월 25일
for k = 1:3
buttons(k) = uicontrol('units','normal','style','pushbutton', ...
'pos',[.23+.18*k .02 .16 .08],'fontweight','bold', ...
'callback','homework(get(gco,''string''))');
It's a string

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by