Question

I've written a fairly large data acquisition program in matlab that receives input data on a pretty tight timing schedule and plots it in real-time on the GUI. My problem is that i need a way for users of the program to view the GUI over the web to monitor their data remotely. My solution was to take a snap shot of the GUI figure every 5 seconds or so and host that image on a web server.

However this leads to two unacceptable problems:

  1. The print function is simply too slow-- it takes ~3-4 seconds for each save and the program subsequently falls behind in it's other routines after each call to 'print'.

  2. For some reason the print function causes the GUI to be temporarily distorted, changing the location of some components and duplicating others. It lasts for just a second or so, but it still makes my solution impractical.

Is there a way to solve either of these issues?

EDIT:*** To anyone interested, the best solution i found was to use an external toolkit called Minicap.

Was it helpful?

Solution 2

For anyone struggling with this in the future the solution I landed on for Windows platforms is a little program called Minicap that allows one to easily capture and save screenshots with system commands.

One needs to have the underlying Windows handle of the figure, which can be accessed using the publicly available matlab function called gethwnd() written by superuser Yair M. Altman.

You can then take a very fast, high resolution screenshot of a figure and save it to disk with something like the following:

winHandle = gethwnd(matlabFigHandle);
cmndstr = sprintf('%s','MiniCap.exe -save ','"',snapShotFileNamePath,'"',...
    ' -compress 9', ' -capturehwnd ', num2str(winHandle),' -exit');
system(cmndstr);

OTHER TIPS

The file format makes a big difference. Here's a sample scatterplot

n = 1e4;
hfig = figure;
hax = plot(1:n, rand(1, n), '+');

...and some timings for saving to different formats.

tic; print(hfig, 'test.bmp', '-dbmp'); toc      %4.1s
tic; print(hfig, 'test.bmp', '-dbmp256'); toc   %2.0s
tic; print(hfig, 'test.png', '-dpng'); toc      %1.9s
tic; print(hfig, 'test.tiff', '-dtiff'); toc    %0.45s
tic; print(hfig, 'test.jpg', '-djpeg'); toc     %0.44s
tic; print(hfig, 'test.wmf', '-dmeta'); toc     %0.42s

tiff, jpeg and wmf were joint first, though tiff files are huge, jpeg quality is lousy and wmf has problems if you aren't on a Windows platform.


For raster formats, the resolution also affects the timing.

tic; print(hfig, 'test600.png', '-dpng', '-r600'); toc   %4.2s
tic; print(hfig, 'test72.png', '-dpng', '-r72'); toc     %0.31s

Another thing to speed up printing is to remove bits of your plot that you don't need. Transparency is computationally instensive, so are legends. Sampling your data rather than plotting it all will save time too.

Does your solution have to be pure Matlab? Use a platform toolkit to take a snapshot of the window holding the GUI, and write that to disk. That's extremely fast, and since it will be in a separate thread should handle your threading issues.

You should probably not capture all the time but only when a request was actually made over the web. Even then, do not capture for each request but only if the last snapshot is older than 5 seconds. That should go a long way towards helping performance.

You could saveas() your figure as a .fig file (which should be rather fast) with the MATLAB instance doing the calculations, then use a different MATLAB instance (possibly on a different computer) to print it to the needed format. This way you avoid delays for your other routines, and the distorions caused by print don't happen on your "main" MATLAB instance.

Note that print is an .m file that you can look into to see if there is anything you can throw out for your particular case. For example, it calls private/prepare.m which I think causes the distortions / position changes you talk about... maybe you could play around with this?

Update: Does the data have to go through a web interface? If the userbase was small enough, maybe you could try to get away with teaching them to use a remote desktop software, such as VNC, Teamviewer or the Terminal Server stuff built into Windows to remotely view their data?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top