Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I realize this is an old thread, but I ran into this problem again lately, so I wanted to summarize my findings. My main source is <a href="http://www.mathworks.com/support/solutions/en/data/1-3NMHJ5/?solution=1-3NMHJ5" rel="nofollow noreferrer">this page</a> (<a href="http://www.webcitation.org/6BgYEekMg" rel="nofollow noreferrer">cached</a>). According to it, there are three alternatives:</p> <ol> <li><p>using <a href="http://www.mathworks.com/help/matlab/ref/avifile.addframe.html" rel="nofollow noreferrer">ADDFRAME</a> directly with the figure handle (without using <a href="http://www.mathworks.com/help/matlab/ref/getframe.html" rel="nofollow noreferrer">GETFRAME</a>). This is exactly what <a href="https://stackoverflow.com/a/4138659/97160">@rescdsk</a> has shown in his answer.</p> <pre><code>hFig = figure('Visible','off'); aviobj = avifile('file.avi'); for k=1:N %#plot(...) aviobj = addframe(aviobj, hFig); end aviobj = close(aviobj); </code></pre></li> <li><p>using <a href="http://www.mathworks.com/help/matlab/ref/print.html" rel="nofollow noreferrer">PRINT</a>/<a href="http://www.mathworks.com/help/matlab/ref/saveas.html" rel="nofollow noreferrer">SAVEAS</a>/<a href="http://www.mathworks.com/help/matlab/ref/hgexport.html" rel="nofollow noreferrer">HGEXPORT</a> to export the figure to an image file, then reading the image back from disk. This is approach#2 that you listed yourself in the question above.</p> <pre><code>hFig = figure('Visible','off'); set(hFig, 'PaperPositionMode','auto', 'InvertHardCopy','off') aviobj = avifile('file.avi'); for k=1:N %#plot(...) print(['-f' num2str(hFig)], '-zbuffer', '-r0', '-dpng', 'file.png') img = imread('file.png'); aviobj = addframe(aviobj, im2frame(img)); end aviobj = close(aviobj); </code></pre></li> <li><p>using the undocumented HARDCOPY function to capture the figure in-memory.</p> <pre><code>hFig = figure('Visible','off'); set(hFig, 'PaperPositionMode','auto') aviobj = avifile('file.avi'); for k=1:N %#plot(...) img = hardcopy(hFig, '-dzbuffer', '-r0'); aviobj = addframe(aviobj, im2frame(img)); end aviobj = close(aviobj); </code></pre> <p>In fact, this is the underlying function that other functions use either directly or indirectly. By inspecting the source codes where possible, here is an illustration of the dependencies of the related functions where <code>A --&gt; B</code> denotes <code>A calls B</code>:</p> <pre><code>saveas [M-file] --&gt; print [M-file] --&gt; render [private M-file] --&gt; hardcopy [P-file] hgexport [P-file] --&gt; print [M-file] --&gt; ... @avifile/addframe [M-file] --&gt; hardcopy [P-file] </code></pre> <p>On the other hand, GETFRAME does not call HARDCOPY but an undocumented built-in function named CAPTURESCREEN (although it seems that it will be using PRINT for the upcoming <a href="http://undocumentedmatlab.com/blog/matlab-hg2/" rel="nofollow noreferrer">HG2 system</a> where there is a new <code>-RGBImage</code> print flag):</p> <pre><code>getframe [M-file] --&gt; capturescreen [builtin] </code></pre></li> </ol> <p>Note: Since <a href="http://www.mathworks.com/help/matlab/ref/avifile.html" rel="nofollow noreferrer">AVIFILE</a> is now deprecated, you can replace it with the newer <a href="http://www.mathworks.com/help/matlab/ref/videowriterclass.html" rel="nofollow noreferrer">VIDEOWRITER</a> in (2) and (3), but not in (1) since it does not support passing figure handle directly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload