Can the start button of a GUI be pressed automatically?

I have a matlab routine that runs a simulation that takes a long time (sometimes many hours). After starting the simulation, I can't use the command window for anything else until the simulation is complete because the simulation does not return to the command prompt until it is done. Sometimes I would start another instance of Matlab running so I could run other Matlab programs while the simulation is running. However, I prefer to have only one instance of Matlab running, so I modified the simulation so that it included a gui with a start button. The call back of the start button starts the simulation. This way, the simulation returns to the command prompt so I can enter other matlab commands while the simulation is running. Although this works well, I was wondering if there was any way the start button could be activated automatically as soon as the program was started. That would be more convenient. Also, at least once, I was dissappointed to find that after coming back several hours later it hadn't made any progress because I had forgotten to press the damn start button.

3 Comments

What simulation, external program or just code that take long to execute?
Consider this example, if your callback for start button looks similar to this
function callbackfunction(src, event)
startfunction()
end
If you call your routine, maybe you could add an additional input argument to it that starts the simulation
function subroutine(argin1, argin2, varargin)
% this port code should be at the end of
if ~nargin % 0
% no inputs, you have to press the damn button
uialert(figHandle, 'Press the damn button', 'A friendly reminder', ...
'Icon', 'warning'); % a nice reminder
else
% there's an input
startfunction()
end
end
Note on uialert, your figure actually has to be uifigure for it to work.
uifigure() did not exist in R2013b that the person is using ;-)
Shoot, I didn't notice the release.

Sign in to comment.

Answers (2)

In that release, you would have been using GUIDE .
In the main .m file, there would be an OpenFcn callback. You will know it is the right one because it will probably have a commented out call to uiwait() or waitfor() .
The OpenFcn callback is invoked after the figure has been completely loaded and the handles structure has been created. It will be invoked automatically when you start the GUI by name or by double-clicking on the .m file for the GUI (do not double-click on the .fig file -- that does not initialize the code properly.)
You can alter the OpenFcn callback to put in a call to the function that does the actual work.
However, if you put in a direct call, and the function goes off and does the work, then the function is not going to return and you will not get back to the command line.
The work-around for that is to have the OpenFcn callback create a timer that invokes the function. Timers run in the background, so you should be able to use the command line.
I wasn't using Guide (which isn't needed especially for such a simple GUI), but I was able to use your suggestion to use the timer object. Interestingly enough it didn't work with the default value (0.0) for StartDelay. It would execute the callback but not return to the command line. But if I put a small value in for StartDelay (such as 0.001) it worked as I had hoped. I'm not sure why that would be, but the fact that I'm wasting a millisecond is certainly not important for a program that takes hours to run :) Many thanks Walter (and to Mario even though I was not able to use his suggestion). ~Paul

Categories

Products

Release

R2013b

Tags

Asked:

on 15 Feb 2021

Answered:

on 15 Feb 2021

Community Treasure Hunt

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

Start Hunting!