C graphics
C graphics

Reputation: 7458

Saving contents of figure to a matrix in Matlab ( whithout using getframe, nor with saving to file)

The function getframe captures whatever is visible on the screen. However in my application I want the figure to be invisible when storing its content in a matrix. So what the getframe does is that, for a short period, it makes the figure visible and captures its contents and then sets the 'visibile' property back to what it was before screen capture. I do not want that flash happening on the screen. As well, saving in file and reading it back reduces speed. There has got to be a way to get around this.

hFig=figure('Visible','off'')
text ('String','ABC','fontsize',300)
imageData = getframe(hFig);       
img = imageData.cdata; % img is what I am interested in

Upvotes: 1

Views: 1784

Answers (1)

Pursuit
Pursuit

Reputation: 12345

The only way I know to do this is to print the figure to a temporary file. For example:

%Create a figure
hFig=figure('Visible','off')
text ('String','ABC','fontsize',300)

%Save the figure to a tiff file using a salted name
tmpName = sprintf('Temp_Figure_%04d.tiff', floor(rand*1000));
print(['-f' num2str(hFig)], '-dtiff', tmpName)

%Read the data
img = imread(tmpName);

%Delete the temporary figure
delete(tmpName);

Not the prettiest thing, but it seems to work.

Upvotes: 2

Related Questions