Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Controlling a GUI from a script?

0 views
Skip to first unread message

Bob

unread,
Jul 20, 2008, 10:14:02 AM7/20/08
to
Hi everyone -

I have what seems to be a simple problem - I'd like to
control a GUI from an interactive script. I know it can't
be too hard, but I haven't looked in the right place yet.
Any ideas?

Bob

Rune Allnor

unread,
Jul 20, 2008, 11:14:36 AM7/20/08
to

What GUI are you talking about? One you made yourself?
One that's been supplied by matlab?

Rune

Bob

unread,
Jul 20, 2008, 12:16:02 PM7/20/08
to

> What GUI are you talking about? One you made yourself?
> One that's been supplied by matlab?
>
> Rune

One I made myself. Fairly self contained number cruncher,
wanted do a parametric study varying over multiple data
sets, but I was too short sighted to think of that ahead of
time.

I wanted to supply filepaths, set a couple switches and call
the main "Go" function, all from an external script.

I'd rewrite it, but I won't get time until after I need this
work done.

Rune Allnor

unread,
Jul 20, 2008, 12:31:27 PM7/20/08
to

I can't think of any other way than rewriting the program to be
called from a script. There are some GUI kits where one can
record actions and run it again, but I don't know if that can
be done with matlab.

On the other hand, you already have all the functional code.
'All' you need to do is to refactor it into functions and add
a wrapper function or two.

One short-term option is to hire a student or intern and have
him or her run the GUI manually...

Rune

Bruno Luong

unread,
Jul 20, 2008, 1:42:01 PM7/20/08
to
"Bob " <bob.k...@dynetics.com> wrote in message
<g5voc2$l2a$1...@fred.mathworks.com>...

Simulate a user mouse action by of calling the callback is
not difficult, there is a mechanism for that (see GUI m file
header).

The hard part (not sure you are right when you wrote "I know
it can't be too hard") is capture the current state machine
of the GUI (something is running, wait for action, etc...),
and launch an appropriate action at the right time. If your
GUI has been programmed with a state machine that is clearly
defined and accessible through variables
, then you probably have to program a timer to check
periodically the state machine of your GUI, and some sort of
function WaitForGuiState('stateA') that must be called in
between two callback actions.

But in general I find the approach awkward.

Usually a program must has been designed with some sort of
engine that can be called by script. The GUI is a super
interface layer for calling the script and run the engine.

Doing the reverse is very unnatural.

Bruno

Jesse Lai

unread,
Jul 20, 2008, 9:28:43 PM7/20/08
to

It is quite simple to run a GUI from a script if the GUI was made in
GUIDE. For example, I made a GUI called mygui.m (with mygui.fig) that
has a checkbox (checkbox1) and a pushbutton (pushbutton_Go). The
following code allows you to set the value of the checkbox and execute
the callback for the pushbutton.

Depending on which version of MATLAB you have, the method to execute the
callback for the pushbutton will differ. In R2008a, callbacks were
changed to function handles, so you have to use them differently.

%--------CODE-----------%
h = mygui; % Returns handle to GUI
handles = guidata(h); % Get handles structure from GUI, now you can code
just like you normally would inside the GUI

% Set the value of the checkbox
set(handles.checkbox1, 'Value', 1)

% Get the callback function to pushbutton_Go
callback = get(handles.pushbutton_Go, 'Callback');
% Run callback, this will depend on what version of MATLAB you're using

% Pre R2008a

% Replace gcbo with handles.figure since gcbo won't exist
callback = strrep(callback, 'gcbo', 'handles.figure1');
eval(callback)

% Post R2008a (callback is returned as function handle)
% don't know if this syntax is right, don't have R2008a installed here
%callback(handles.pushbutton_Go, []);

0 new messages