MATLAB: How to export the whole figure data into MATLAB workspace

exportfigureimshowinvisiblematlab plot

Hi, I have an invisible figure in which I have 2 color images (displayed by imshow) and some red points (displayed by plot) on the image. I want to save the image with those points to a new variable in MATLAB in workspace. Up to now, I have been succeeded to export the whole figure data to a jpg file with print command. But since I am then forced to read that JPG file, it makes the process slow. Also, I tried getframe(figure_handle), but when it runs, the figure is displayed and so it is not suitable. I am looking for something similar to im = getframe(figure_handle) but in a way that figure is not displayed. I would really appreciate any help of yours to solve this problem.

Best Answer

You can call the undocumented function hardcopy. But there are some pitfalls, e.g. the EraseMode of all objects must be 'normal', otherwise hardcopy crashs.
% Simulate PRINT command (save time for writing and reading image file):
% Set units of axes and text from PIXELS to POINTS to keep their sizes
% independent from from the output resolution:
% See: graphics/private/preparehg.m
root_SHH = get(0, 'ShowHiddenHandles');
set(0, 'ShowHiddenHandles', 'on');
text_axes_H = [findobj(FigH, 'Type', 'axes'); ...
findobj(FigH, 'Type', 'text')];
pixelObj = findobj(text_axes_H, 'Units', 'pixels');
fontPixelObj = findobj(text_axes_H, 'FontUnits', 'pixels');
set(pixelObj, 'Units', 'points');
set(fontPixelObj, 'FontUnits', 'points');
% Set image driver:
if strcmpi(fig_Renderer, 'painters')
imageDriver = '-dzbuffer';
else
imageDriver = ['-d', fig_Renderer];
end
fig_ResizeFcn = get(FigH, 'ResizeFcn');
set(FigH, 'ResizeFcn', '');
% "Normal" is the only erasemode, which can be rendered!
% See: NOANIMATE.
EraseModeH = [ ...
findobj(FigH, 'EraseMode', 'xor'); ...
findobj(FigH, 'EraseMode', 'none'); ...
findobj(FigH, 'EraseMode', 'background')];
EraseMode = get(EraseModeH, {'EraseMode'});
set(EraseModeH, 'EraseMode', 'normal');
% Get image as RGB array:
high = hardcopy(FigH, imageDriver, '-r300');
% Restore units of axes and text objects, and EraseMode:
set(pixelObj, 'Units', 'pixels');
set(fontPixelObj, 'FontUnits', 'pixels');
set(EraseModeH, {'EraseMode'}, EraseMode);
set(0, 'ShowHiddenHandles', root_SHH);
set(FigH, 'ResizeFcn', fig_ResizeFcn);