Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: GUI progressively getting slower

I've been programming some MATLAB GUIs (not using GUIDE), mainly for viewing images and some other simple operations (such as selecting points and plotting some data from the images).

When the GUI starts, all the operations are performed quickly. However, as the GUI is used (showing different frames from 3D/4D volumes and perfoming the operations mentioned above), it starts getting progressively slower, reaching a point where it is too slow for common usage.

I would like to hear some input regarding:

  • Possible strategies to find out why the GUI is getting slower;
  • Good MATLAB GUI programming practices to avoid this;
  • Possible references that address these issues.

I'm using set/getappdata to save variables in the main figure of the GUI and communicate between functions.

(I wish I could provide a minimal working example, but I don't think it is suitable in this case because this only happens in somewhat more complex GUIs.)

Thanks a lot.

EDIT: (Reporting back some findings using the profiler:)

I used the profiler in two occasions:

  • immediatly after starting the GUI;
  • after playing around with it for some time, until it started getting too slow.

I performed the exact same procedure in both profiling operations, which was simply moving the mouse around the GUI (same "path" both times).

The profiler results are as follows:

enter image description here

I am having difficulties in interpreting these results... Why is the number of calls of certain functions (such as impixelinfo) so bigger in the second case?

Any opinions?

Thanks a lot.

like image 629
fnery Avatar asked May 15 '13 14:05

fnery


People also ask

Why is MATLAB suddenly slow?

MATLAB may be running slowly because you have a limited amount of RAM (i.e. under 128MB). The RAM used by MATLAB at runtime is between 40MB-60MB. The HELP browser can take up another 12MB. If you have limited memory (RAM), your processor may start using virtual memory (from your hard drive).

Why does MATLAB take so long to run?

A slow startup is often caused by issues with the license search path. You probably need to adjust one of the license environment variables (LM_LICENSE_FILE or MLM_LICENSE_FILE) used by MATLAB, or else bypass them all together.


2 Answers

The single best way I have found around this problem was hinted at above: forced garbage collection. Great advice though the command forceGarbageCollection is not recognized in MATLAB. The command you want is java.lang.System.gc()... such a beast.

I was working on a project wherein I was reading 2 serial ports at 40Hz (using a timer) and one NIDAQ at 1000Hz (using startBackground()) and graphing them all in real-time. MATLAB's parallel processing limitations ensured that one of those processes would cause a buffer choke at any given time. Animations would not be able to keep up, and eventually freeze, etc. I gained some initial success by making sure that I was defining a single plot and only updating parameters that changed inside my animation loop with the set command. (ex. figure, subplot(311), axis([...]),hold on, p1 = plot(x1,y1,'erasemode','xor',...); etc. then --> tic, while (toc<8) set(p1,'xdata',x1,'ydata',y1)...

Using set will make your animations MUCH faster and more fluid. However, you will still run into the buffer wall if you animate long enough with too much going on in the background-- especially real-time data inputs. Garbage collection is your answer. It isn't instantaneous so you don't want it to execute every loop cycle unless your loop is extremely long. My solution is to set up a counter variable outside the while loop and use a mod function so that it only executes every 'n' cycles (ex. counter = 0; while ()... counter++; if (~mod(counter,n)) java.lang.System.gc(); and so on.

This will save you (and hopefully others) loads of time and headache, trust me, and you will have MATLAB executing real-time data acq and animation on par with LabVIEW.

like image 159
Joseph Avatar answered Oct 03 '22 08:10

Joseph


A good strategy to find out why anything is slow in Matlab is to use the profiler. Here is the basic way to use the profiler:

profile on
% do stuff now that you want to measure
profile off
profile viewer

I would suggest profiling a freshly opened GUI, and also one that has been open for a while and is noticeably slow. Then compare results and look for functions that have a significant increase in "Self Time" or "Total Time" for clues as to what is causing the slowdown.

like image 25
shoelzer Avatar answered Oct 03 '22 08:10

shoelzer