How can I click a button in a .fig from a script or function?

조회 수: 7 (최근 30일)
Riccardo Celin
Riccardo Celin 2021년 3월 9일
댓글: Riccardo Celin 2021년 3월 10일
Hello everybody,
I was searching for a way to click on a button in a .fig with the mouse from an external script (this because I only have the .p versione of the code... ). I fond on prevoius topics these commands:
import java.awt.Robot;
import java.awt.event.*;
robot = Robot();
robot.delay(2000);
robot.mouseMove(600,600);
...but this seems not to work on my Mac (matlabR2020), I also tried from MatlabOnline on a Windows PC.
So I was wondering if there is an alternative method for "pressing" a button of a .fig from an external script.
Can you please help me with this problem?
Thank you so much!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 9일
You can openfig() the fig file and assign the result (figure number) to a variable, and
findall(FigureNumber, 'type', 'uicontrol', 'style', 'pushbutton')
This will return handles to all of the pushbuttons. You can then examine their properties such as String to locate the one you want. That handle will have a callback property that will contain an anonymous function in which you will be able to see the function being invoked. It will look something like
@(hObject, event) GUINAME('FUNCTIONNAME', hObject, event, handles(gcbo))
You need the gui name and function name from that, and you need unique identifying information about the button, with it being quite likely that the button Tag will be unique.
Now, the process of "clicking" the button is:
  1. run the gui by name, recording the figure handle returned. It is possible that the gui does not return until you quit the gui: if so then that complicates matters, potentially even too much for this general strategy to be used, but other times just a durned nuisance.
  2. assuming that the figure handle was returned, use findall() on it with the identifying information determined earlier, probably the Tag, to get the handle to the button object
  3. when it is time to "click" the button, first set the Value property of the button to 1
  4. then call GUINAME('FUNCTIONNAME', buttonhandle, [], handles(buttonhandle)) where GUINAME and FUNCTIONNANE are copied from your investigation
  5. then set the button handle Value back to 0
The exact sequence of steps I give here would need to be modified if the gui is sufficiently old that the Callback was not an anonymous function and was instead a character vector
If the gui does not return until it is quit then you have a challenge on your hands of needing to initialize the gui without calling the OpenFcn callback. The OpenFcn could potentially be spring loaded to set variables in ways that could be detected by the code to validate that regular initialization was done, if the gui went to enough trouble.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by