Matlab locks up when saving figure with color map using scatter()

1 view (last 30 days)
I have a Matlab program that generates 3 figures: 2 use the plot() function and the 3rd uses the scatter() function which color maps the points based on a 3rd dimension. The plot() types all display fine and the first 2 I save as jpeg files using print() with no issues; however, when I try to save the 3rd colormapped plot to jpeg using print(), the process is extremely slow (an hour vs. seconds for the normal plots) and I have to use windows task manager to close it if I don't want to wait. If I go to the File menu in the figure itself and try to save as jpeg it does the same thing. I attached the .fig file to this post (below). Using saveas() causes the same issue. The second to last line, "print('-djpeg', file_jpg, '-r300')", is where it bogs down. Is there an alternative method to print() or saveas() that might be faster?
I'm using Matlab R2014a with Windows 7.
figure
subplot(2,1,1);
plot(sec_from_start, WVSS2F_VMR, sec_from_start, WVSS2R_VMR,'-g', sec_from_start, VMR_C_U,'cx',sec_from_start, VMR_CR2,'-k', sec_from_start, wmr_GE_s,'-y');
hold on; %holds plot and axis properties
plot( sec_from_start, PALT_RVS,'.r')
title(file,'Interpreter','none')
TAS_thresh=100; %start plot when TAS exceeds a threshold (m/s)
for i = 1:length(PALT_RVS)
if TAS(i) > TAS_thresh
time_plot_start = sec_from_start(i); %start time for plots
break
end
end
axis([time_plot_start, max(sec_from_start), 0, 14000])
l=legend(firsty,secondy,thirdy,forthy,fifthy,sixy); set(l,'Interpreter','none')
xlabel('time (sec from start)')
ylabel('PPMv, Palt (m)')
grid
WVSS_ERR = WVSS2F_VMR - wmr_GE_s; %WVSS2 minus GE chilled mirror
subplot(2,1,2)
[AX, H1, H2] = plotyy(sec_from_start, WVSS2F_VMR, sec_from_start, WVSS_ERR);
%set(H2, 'Marker','.');
set(H2, 'Color','r');
set(get(AX(1),'Ylabel'),'String','PPMv'); %set left Y-axis
set(get(AX(2),'Ylabel'),'String','PPMv error'); %set right Y-axis
set(AX(1),'xlim',[time_plot_start, max(sec_from_start)]);
set(AX(2),'xlim',[time_plot_start, max(sec_from_start)]);
set(AX(2),'ylim',[-2000 2000]);
set(AX(2),'YTick',[-2000 -1000 0 1000 2000]);
xlabel('time (sec from start)')
%title(file,'Interpreter','none')
l=legend(firsty, 'error (WVSS2F-GE chilled mir.)'); set(l,'Interpreter','none')
grid
file_jpg = [path_file, '_out.jpg'];
print('-djpeg', file_jpg, '-r300'); %save figure to file with resolution rXXX
figure
[H] = plot(wmr_GE_s, WVSS2F_VMR);
set(H, 'Color','b'); set(H, 'marker', '.'); set(H, 'line', 'none')
ylabel('WVSS2 (PPMv)'); %set left Y-axis
xlabel('GE chilled mirror (PPMv)')
axis([0, 40000, 0, 40000])
title(file,'Interpreter','none')
l=legend('WVSS2F vs. GE chilled mir.'); set(l,'Interpreter','none')
set(gca,'xTick',0:5000:40000)
set(gca,'yTick',0:5000:40000)
grid
file_jpg = [path_file, '_scatter_out.jpg'];
print('-djpeg', file_jpg, '-r300'); %save figure to file with resolution rXXX
hold on;
figure
%%plot with colormap
colormap(jet); %defines bype of color map for points in scatter plot
cmin = 0; cmax = 200; V=[cmin, cmax];
scatter1 = scatter(wmr_GE_s,WVSS2F_VMR,10,TAS_RVSM);
caxis(V); %set color map range
hcb = colorbar('YTick', [cmin 0 cmax],'YTickLabel', {num2str(cmin),' Palt=0 m', num2str(cmax)}); %add colorbar scale to plot
ylabel('WVSS2 (PPMv)'); %set left Y-axis
xlabel('GE chilled mirror (PPMv)')
axis([0, 40000, 0, 40000])
set(gca,'xTick',0:5000:40000)
set(gca,'yTick',0:5000:40000)
title('test','Interpreter','none')
grid
file_jpg = [path_file, '_scatter_color_out.jpg'];
print('-djpeg', file_jpg, '-r300'); %save figure to file with resolution rXXX
%saveas(gcf, file_jpg); %save figure to file with resolution rXXX

Answers (1)

Abhinaya Kennedy
Abhinaya Kennedy on 2 Sep 2024
Edited: Abhinaya Kennedy on 2 Sep 2024
Problems with saving the scatter plot as a JPEG file is likely due to the complexity of the plot, especially with the colormap and the potentially large number of data points. You could try
  • Breaking plots into smaller sections and saving those separately, then combining them if needed might help.
  • Reducing the resolution of the saved image. Instead of -r300, you could try -r150 or -r100 to see if this speeds up the process.
  • Using a different file format that might handle complex plots better, such as PNG or TIFF. You can do this by changing '-djpeg' to '-dpng' or '-dtiff' in the print function.
  • Reducing the number of data points in your scatter plot or simplifying the colormap to see if that has any effect.
  • Updating to a more recent version of MATLAB, as newer versions often have performance improvements and bug fixes. If you have access to a newer version of MATLAB, consider using the exportgraphics function, which can handle complex graphics more efficiently.
% Assuming the figure is already created
file_jpg = [path_file, '_scatter_color_out.png']; % Change to PNG
print('-dpng', file_jpg, '-r150'); % Save with lower resolution

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!