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

6 ビュー (過去 30 日間)
Fares
Fares 2018 年 1 月 28 日
編集済み: Yair Altman 2018 年 1 月 29 日
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 Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by