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

35 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-1-28
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.

请先登录,再进行评论。

回答(2 个)

Yair Altman
Yair Altman 2018-1-29
编辑:Yair Altman 2018-1-29

Jan
Jan 2018-1-29
编辑:Jan 2018-1-29
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.

类别

Help CenterFile Exchange 中查找有关 Display Image 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by