one multiplot figure instead of many partially filled figures

41 Ansichten (letzte 30 Tage)
Elzbieta
Elzbieta am 20 Nov. 2024 um 19:29
Bearbeitet: Madheswaran am 20 Nov. 2024 um 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

Antworten (1)

Madheswaran
Madheswaran am 20 Nov. 2024 um 19:52
Bearbeitet: Madheswaran am 20 Nov. 2024 um 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!

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by