What is the fastest way to export a figure in MATLAB?

36 views (last 30 days)
I am trying to save a figure as a .png using:
saveas(plot_handle,'temp_figure.png','png' )
This takes about 0.8 seconds to save the figure. Is there a way I can export the figure as an image faster?
Details about what I'm trying to do:
I wrote a matlab function that produces a plot. I used the matlab compiler to compile that function as a .dll, and use it within a body of c++ code. The c++ code is essentially a GUI made with MFC, and one of the buttons calls the matlab function from the .dll. My problem is that I want the plot to show up on the GUI. The way I do this is make the matlab function save the figure as an image (png or bitmap), and then let the c++ code load that image and put it on the GUI. This works, but it is too slow because it takes 0.8 seconds to save the figure as an image.
  1 Comment
Walter Roberson
Walter Roberson on 28 Jan 2018
You could try export_fig from the file exchange, and you could try print(), but I am not sure that either one will be faster.

Sign in to comment.

Answers (2)

Yair Altman
Yair Altman on 29 Jan 2018
Edited: Yair Altman on 29 Jan 2018

Jan
Jan on 29 Jan 2018
Edited: Jan on 29 Jan 2018
See FEX: savepng for a faster method to write a PNG file.
But sharing data between processes using the slow disk as a channel is a bad idea in general. The compression of the PNG format takes time also twice: for encoding and decoding. You could get the screen's contents by
cdata = print('-RGBImage', '-r0')
But this is not fast also. I assume this is better:
function RGB = FigShot(FigH)
screen = get(groot, 'ScreenSize');
pos = getpixelposition(FigH);
robot = java.awt.Robot;
rect = java.awt.Rectangle(pos(1), screen(4) - pos(4) - pos(2), pos(3), pos(4));
jImage = robot.createScreenCapture(rect);
h = jImage.getHeight;
w = jImage.getWidth;
pixel = reshape(typecast(jImage.getData.getDataStorage, 'uint8'), 4, w, h);
RGB = cat(3, transpose(reshape(pixel(3, :, :), w, h)), ...
transpose(reshape(pixel(2, :, :), w, h)), ...
transpose(reshape(pixel(1, :, :), w, h)));
end
But I do not have a smart idea of how to get the image data to your other process.

Categories

Find more on Display Image 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!