Question

I'm trying to save .jpg files from MATLAB, but I don't want to have the figure pop up every time since this really slows down the process. However, setting 'visible' to 'off' doesn't seem to work. What can I do to get the Figure window to not pop up?

nFrames = 2557; % Number of frames. Number of days between 1/1/2008 and 1/31/2014
for k = 183:nFrames % 183 (7/1/2008) to the number of days. Data before this is either missing or from HI 
    % Map of conterminous US
    ax = figure(1);
    set(ax, 'visible', 'off', 'units','normalized','outerposition',[0 0 1 1]) %  Make window that shows up full sized, which makes saved figure clearer
    ax = usamap('conus');
    states = shaperead('usastatelo', 'UseGeoCoords', true,...
        'Selector',...
        {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
    faceColors = makesymbolspec('Polygon',...
        {'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
    geoshow(ax, states, 'DisplayType', 'polygon', ...
        'SymbolSpec', faceColors)
    framem off; gridm off; mlabel off; plabel off

    hold on

    % Plot data
    scatterm(ax,str2double(Lat_PM25{k}), str2double(Lon_PM25{k}), 40, str2double(data_PM25{k}), 'filled'); % Plot a dot at each Lat and Lon with black outline around each dot (helps show dots that are too low for it to be colored by the color bar
        % Draw outline around dot with 'MarkerEdgeColor', [0.5 0.5 0.5] for gray
    hold on

    % Colorbar
    caxis([5 30]);
    h = colorbar;
    ylabel(h,'ug/m3');

    % Title
    % date = datenum(2007, 04, 29) + k; % Convert t into serial numbers.
    title(['PM2.5 24-hr Block Average Concentration ', datestr(cell2mat(date_PM25(k)), 'mmm dd yyyy')]); % Title changes every day;

    % Capture the frame
    mov(k) = getframe(gcf);

    % Set size of paper
    set(gcf,'Units','points')
    set(gcf,'PaperUnits','points')

    size = get(gcf,'Position');
    size = size(3:4);
    set(gcf,'PaperSize',size)

    set(gcf,'PaperPosition',[0,0,size(1),size(2)])

    % Save as jpg (just specify other format as necessary) - Must set 'facecolor' to 'none' or else color of states turn out black
    eval(['print -djpeg map_US_' datestr(cell2mat(date_PM25(k)),'yyyy_mm_dd') '_PM25_24hrBlkAvg.jpg']);

    clf
end
Was it helpful?

Solution

The problem is with getframe. A detailed answer was given in this thread and in this discussion. In short you need to avoid getframe and instead do something like the following:

    ax = figure(1); 
    set(ax, 'visible', 'off')
    set(ax, 'PaperPositionMode','auto')
    aviobj = avifile('file.avi');
    ....


   for k=1:N
      %# all the plotting you did before the getframe
      img = hardcopy(ax, '-dzbuffer', '-r0');
      aviobj = addframe(aviobj, im2frame(img));
   end

   aviobj = close(aviobj);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top