MATLAB: How to use the MATLAB Profiler to profile the GUI’s callbacks

callbacksMATLABprofile

How can I use the MATLAB Profiler to profile my GUI's callbacks?
I would like to be able use the profiler to profile my callback functions. How can I do this?

Best Answer

If you enter the code you want to run in the Profiler, it will stop automatically when the code is finished. However, you are not required to enter the code to be run, so you should be able to profile the callback functions without entering any code. Instead, start the Profiler, then interact with the uicontrol whose callback you want to profile. Click on the "Stop Profiling" button when you are finished.
Alternately, if you want the Profiler to automatically stop when the Callback is finished, you must get the string that is used for the uicontrol's Callback and the handle for the uicontrol itself. You can copy the string from the Property Inspector while the GUI is open within GUIDE. It should look something like this:
mygui('uicontrol1_Callback',gcbo,[],guidata(gcbo))
Next, you will need to start the GUI and get the handle for the uicontrol whose Callback you wish to profile. First, start the GUI from the command line, and save the handle for the figure. The handle for the figure should be the default output if you have not altered the GUI's "OutputFcn" property:
hfig = mygui;
Once you have the figure's handle, you can use the GUIDATA function to obtain the "handles" structure, that contains the handles for the uicontrols in the GUI's figure:
handles = guidata(hfig);
Now that you have the "handles" structure, you can get the handle for the uicontrol, and use it as an input (rather than using the GCBO command) to execute the Callback. You can then enter something like this:
mygui('uicontrol1_Callback',handles.uicontrol1,[],guidata(handles.uicontrol1))
within the Profiler, and then start the Profiler. It will stop automatically when the Callback is finished.