one multiplot figure instead of many partially filled figures
37 views (last 30 days)
Show older comments
Hello,
How to correct my code to obtain one multiplot figure instead of many partially filled multiplot figures??
heart_rate = {'30', '40', '45', '60', '80', '90', '100', '120', '140', '160', '180', '200', '220',...
'240', '260', '280', '300'}
figure;
tiledlayout(2, 4, TileIndexing='columnmajor')
for kleads = 1:length(ECGLeads)
nexttile
sample_snr{kleads} = snr_ecg_leads(:, kleads)
sample_heart_rate = str2double(heart_rate)
plot(sample_heart_rate, snr_ecg_leads(:, kleads));
xlabel('Heart Rate (bpm)');
ylabel('SNR (dB)');
title([Leads(kleads)]);
output_fig_heart = [path_data_fig_heart_rates, Leads{kleads},'.fig']
output_fig_heart_jpg = [path_data_fig_heart_rates, Leads{kleads},'.jpg']
saveas(gcf, output_fig_heart, 'fig')
saveas(gcf, output_fig_heart_jpg, 'jpg')
%grid on;
end %kleads
0 Comments
Answers (1)
Madheswaran
about 21 hours ago
Edited: Madheswaran
about 18 hours ago
Hi @Elzbieta
The behavior you are facing is because you are saving figure (using 'saveas') inside the loop. Each 'saveas' call is saving the entire figure while it's still being populated. This results in multiple partial figures being saved, rather than one complete figure.
To solve this problem, you need to move all the 'saveas' commands outside the plotting loop after all subplots are created.
% ... Existing code
figure;
tiledlayout(2, 4, 'TileIndexing', 'columnmajor')
for kleads = 1:length(ECGLeads)
nexttile
sample_snr{kleads} = snr_ecg_leads(:, kleads);
sample_heart_rate = str2double(heart_rate);
plot(sample_heart_rate, snr_ecg_leads(:, kleads));
xlabel('Heart Rate (bpm)');
ylabel('SNR (dB)');
title(Leads{kleads});
end
% Then save the complete figure after all subplots are done
output_fig_heart = [path_data_fig_heart_rates, 'multiplot.fig'];
output_fig_heart_jpg = [path_data_fig_heart_rates, 'multiplot.jpg'];
saveas(gcf, output_fig_heart, 'fig');
saveas(gcf, output_fig_heart_jpg, 'jpg');
The above code would produce a single image with all subplots properly arranged in a tiled layout instead of multiple partial figures.
Hope this helps!
0 Comments
See Also
Categories
Find more on Printing and Saving 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!