MATLAB Answers

How do I automatically run code in a GUI upon startup?

173 views (last 30 days)
Paul
Paul on 28 Apr 2011
Commented: Joseph Musante on 12 Feb 2019
I am trying to create a monitoring GUI that will have to run without human intervention and I need it to start running the embedded code without a user having to push a button or click anything. It just needs to run as soon as its opened. Where do I put my code in the main GUI code? Under what callback or function should I place my code so that it will run with no human input?
Also, there is a single text box in the GUI that has a list of email addresses that the main code needs, which may be updated by a user at some time after the code has already started running. So the main code needs to be placed after the handles have been updated, so after all of the createFcn's have been run.
Thanks in advance for any help!

  0 Comments

Sign in to comment.

Accepted Answer

More Answers (6)

leeazlee
leeazlee on 7 Sep 2016
put pause(0.005) after line varargout{1} = handles.output; in output function
eg:
function varargout = guidetest_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
pause(0.001)
yourfunction

  1 Comment

Zhengyi
Zhengyi on 20 Apr 2018
This works for normal cases where uiwait is not implemented for the figure.

Sign in to comment.


Maxx Chatsko
Maxx Chatsko on 27 Jul 2011
Putting the code in the OpeningFcn will not solve the problem. Since OpeningFcn "runs just before the GUI is displayed", all code in the OpeningFcn will certainly run, but the GUI will not appear.
It seems that the code will need to be placed after the OpeningFcn if you want the GUI to appear on the screen.
I tried placing a checkbox on the GUI with visibility "off", then setting the value to TRUE in the OpeningFcn, and finally placing my code in the callback for the checkbox. This does not trigger the callback however.
If anyone has a solution for making the GUI run upon opening I would greatly appreciate your feedback. Thanks!

  5 Comments

Show 2 older comments
Paulo Silva
Paulo Silva on 27 Jul 2011
What you need to do is show a message or some kind of logo or even a progress bar to show the user that the GUI is initializing, just like many commercial software, when your calculations are done the GUI appears and you delete the message (just delete the handle of the object) or whatever you put there.
Sorry about my last comment, you said 'but the GUI will not appear', that's partially true because your calculations seem to take some time to run.
Another alternative would be to create and start one timer in the OpeningFcn, whoes callback would start the calculations after X seconds (just the timer delay would work). GUI appears and X time later the calculations are done automatically but that would annoy the user unless you do the same trick I told you (message, progress bar, whatever).
Maxx Chatsko
Maxx Chatsko on 27 Jul 2011
Ah I see thanks for clearing that up. Unfortunately I am looking for a way for the GUI to appear upon opening AND while continuosly calculating. My program will run indefinately on a machine and I was hoping the GUI could show the user various statistics on calculations being performed.
I guess this is not possible? =(
Paulo Silva
Paulo Silva on 27 Jul 2011
MATLAB only performs one task but surely you can do some workarounds, buttons to control the calculations, using the pause and drawnow functions to update the graphs...
There are also people opening MATLAB twice and having both communicating by some way like local network or files, one runs the calculations and another shows the results to the user.

Sign in to comment.


Maxx Chatsko
Maxx Chatsko on 27 Jul 2011
The line preceding the OpeningFcn call reads:
% --- Executes just before myfun is made visible.

  0 Comments

Sign in to comment.


Jeff
Jeff on 27 Jul 2011
I put all of my initialization code in the OpeningFcn function line. Here is a snippet.
function CyberCalcMain_OpeningFcn(hObject, eventdata, handles, varargin)
global bd
% Choose default command line output for CyberCalcMain
handles.output = hObject;
%Initialize and load beam data into global variable
load('BeamData');
% Update handles structure
guidata(hObject, handles);
set(handles.oad2radiuscntrl,'Value',6);

  1 Comment

Paulo Silva
Paulo Silva on 27 Jul 2011
I also do that but some people need to do tasks that take some time and the GUI figure only appears when all the OpeningFcn code is done.

Sign in to comment.


Romesh
Romesh on 29 Oct 2011
Well my problem is that I want to incorporate a scroll bar using Project Waterloo. The scrollbar seems to only render correctly if the parent figure has already been drawn on the screen, so if I try and create the scroll bar object from within the OpeningFcn it fails because the GUI is yet to be rendered

  0 Comments

Sign in to comment.


Yann Vonderscher
Yann Vonderscher on 31 May 2016
I ran into the same issue. I made a GUI that automatically starts calculation on a bunch of files located on a dedicated location. The GUI is there to indicate the status of the calculation, and indicate the name and date of the latest processed file.
It seems that the _OutputFcn(hObject, eventdata, handles) is called only once the GUI is fully loaded (i.e. GUI is visible, and all callbacks are set). Therefore, placing the call to the calculation scripts in this function solves my problem (so far, not fully tested).
Hope that helps.
YAV

  1 Comment

Joseph Musante
Joseph Musante on 12 Feb 2019
This is exactly what I was looking for, Thanks!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!