one multiplot figure instead of many partially filled figures

41 visualizaciones (últimos 30 días)
Elzbieta
Elzbieta el 20 de Nov. de 2024 a las 19:29
Editada: Madheswaran el 20 de Nov. de 2024 a las 20:05
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

Respuestas (1)

Madheswaran
Madheswaran el 20 de Nov. de 2024 a las 19:52
Editada: Madheswaran el 20 de Nov. de 2024 a las 20:05
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!

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by