Profiling Matlab GUI functions
One of the most powerful tools provided by Matlab as an IDE is the profiler, which lets you view performance and diagnostic information about your code. In particular, it shows which lines ran, how many times, and how long each call took - which makes it very easy to diagnose poor performance, and to check the execution path when debugging conditional statements. Typical usage is to open the profiler
profile viewer
and then type the function call or script into the command field in the profiler window. Pressing ‘Start Profiling’ then executes the function/script, and automatically stops when the script ends or the function returns. But what if you want to profile a GUI function or callback?
Luckily, the profiler already has you covered, although it’s a little deceptive. If you leave the command field in the profiler empty, you might expect that clicking ‘Start Profiling’ would result in an error message, because you didn’t specify what command you wanted analyzed. But actually, this will just start profiling all activity in the Matlab session. Profiling continues indefinitely until you manually stop profiling. So to profile a GUI function, you can set up your GUI, then start profiling without entering a command, click around, and the manually stop profiling after you’ve performed the tasks you want analyzed. Of course, the total time would likely include other bits of code that you aren’t interested in, but this doesn’t matter because typically you would only be interested in the analysis for a particular callback, which can be isolated in the profiler output.