How to export the whole figure data into MATLAB workspace

22 views (last 30 days)
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.
  1 Comment
sayedm
sayedm on 18 Nov 2011
figure1_handle = figure('Visible','off');
subplot(1,2,1);
imshow(I);
x = 1:1:10;
y = x;
plot(x,y,'kx','MarkerSize',10,'LineWidth',2);
subplot(1,2,2);
imshow(I);
x = 10:1:20;
y = x;
plot(x,y,'kx','MarkerSize',10,'LineWidth',2);
Ichanged = getframe(figure1_handle); % this line makes the figure visible which is not wanted here, any solution to keep this figure invisible while exporting data into workspace?
figure;imshow(Ichanged); % testing if the figure 2 is similar to that of figure 1

Sign in to comment.

Accepted Answer

Jan
Jan on 18 Nov 2011
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);
  1 Comment
sayedm
sayedm on 19 Nov 2011
Thanks Jan,
It perfectly worked. However, there is only one point about fig_renderer which is not clear to me. There is no function with such name in MATLAB R2011b as I see. I set the imageDriver to -dzbuffer and I reached what I wanted however.
Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!