one multiplot figure instead of many partially filled figures
42 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Madheswaran
2024-11-20,19:52
编辑:Madheswaran
2024-11-20,20:05
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!