I have a function tied into a pushbutton, but I want it to run automatically.
Show older comments
Ive spent about 3 hours trying to figure out how to do this. I am not a matlab expert, I just need the matlab program to run automatically without needing the user to push the button. All the variables and everything else loads automatically, I just wish it would automatically push the button too.
Answers (2)
per isakson
on 26 Feb 2014
Edited: per isakson
on 26 Feb 2014
Disclaimer: I never use GUIDE. However, to know what I miss, I sometimes try it.
Assumtion: you use GUIDE.
Here is one way of achieving an automatic "push" directly after the start of the app.
- In GUIDE assign a unique string value, e.g. tag_of_your_button, to the property, Tag, of your pushbutton.
- write a four line script (or rather function), which starts your app and "pushes" the button. The lines are:
fh = name_of_your_app;
uih = findobj( fh, 'Tag', 'tag_of_your_button' );
foo_handle = get( uih, 'Callback' );
foo_handle( uih, event_data )
- You may use the variable, event_data, to pass some data to the callback_function. Or pass a dummy value.
- Tell the users to use your script to start the app
Not that obvious :-(
This script will probably need some error handling not to cause you embarrassment.
- assert that fh is not empty and is a figure handle
- assert that uih is a pushbutton handle
- assert that foo_handle if a function handle
1 Comment
per isakson
on 26 Feb 2014
I changed
foo_handle( fh, event_data )
to
foo_handle( uih, event_data )
Image Analyst
on 26 Feb 2014
0 votes
I use GUIDE. Whatever code you'd put into your pushbutton callback, just put that into your OpeningFcn() function instead. It will run as soon as you launch the GUI.
3 Comments
per isakson
on 26 Feb 2014
Edited: per isakson
on 26 Feb 2014
Yes, but do not duplicate code (DRY). Put the line
name_of_your_pushbutton_Callback(hObject,[],handles)
as the last line of OpeningFcn().
Image Analyst
on 26 Feb 2014
Edited: Image Analyst
on 26 Feb 2014
I was thinking of doing away with the pushbutton altogether. If it was only needed so he could start running that chunk of code, and it's now in the opening function, why is it needed any more? If it is, then just put the contents of that function into another function, say called "MyStartupCode" or whatever. Then have both the opening function and the button callback call that MyStartupCode function.
Also, I'm not sure hObject would be the handle ID to the pushbutton if you called it from the opening function. The OpeningFcn has it's own copy of hObject which would then be passed into name_of_your_pushbutton_Callback(), instead of the handle ID of the pushbutton, right? I never, ever use hObject - I always use the actual, known tag of the control when I refer to control properties.
per isakson
on 26 Feb 2014
Edited: per isakson
on 26 Feb 2014
The "spec" in the Question is open for interpretations. Why a GUI in the first place?
"MyStartupCode", yes it is probably a good idea to move code from the main m-file, name_of_your_app, to separate functions. These might even be possible to reuse.
"I'm not sure hObject [...]" The value of hObject will be the handle of the main figure. I silently assumed that hObject would not be used. Maybe
name_of_your_pushbutton_Callback(nan,[],handles)
would have been better.
I use the solution, which I described in my answer, in automatic testing of GUIs. And my answer addresses the Question as stated in the title.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!