Save images in for loop without overwrite

조회 수: 7 (최근 30일)
Rachael Courts
Rachael Courts 2020년 6월 15일
댓글: Rachael Courts 2020년 6월 16일
Hi All,
I'm currently running a loop through wav files, running spectrograms and wanting to save the spectrogram figure as a png file. I am wanting to save the images in a specific file but cant seem to work that out. I want the png file name to match the 'fname' also but cant work that out. The code I'm using does save the figure into the 'current folder' window of MATLAB, but as soon as the loop runs through again that file is replaced by the consecutive figure. Any help would be great. Please said my code below.
for i = 1: length(d); % loop over wav files
[pathstr,fname,ext] = fileparts(d(i).name);%fname is file name
[wf, fs] = audioread([WavDir, d(i).name]); % read 1 wav file at a time: waveform and fs
p = wf(:,ch2use); clear wf; % if multiple channels, pressure p is now just one channel
callin = 10.^(abs(cal)/20); p2 = p * callin; clear p; % p2 is now calibrated pressure
NFFT = fs; % window length = 1 s if NFFT = fs
[S,F,T,P] = spectrogram(p2,NFFT,NFFT/2,NFFT,fs,'yaxis');
% spectrogram(X,WINDOW,NOVERLAP,NFFT,Fs)
PSD = 10*log10(P); % dB
figure(1); clf; h = pcolor(T, F, PSD); set(h,'EdgeColor','none');
shading interp; set(gcf,'Renderer','zbuffer'); set(gca, 'YScale', 'log');
caxis([40 120]); colormap(jet); set(gca,'ylim',[10 fs/2]);
set(gca,'tickdir','out');
xlabel('Time [s]'); ylabel('Frequency [Hz]'); title(fname);
h1 = colorbar('peer',gca); set(get(h1,'ylabel'),'String','PSD [dB re 1 \muPa^2/Hz]');
save([ResDir,fname,'.mat'],'PSD', 'F', 'T', 'fs')
fname % write to screen to monitor which file is being processed
saveas(figure(1),'fname.png')
clf(figure(1))
end % loop over wav files
Also other code I've attempted:
%worked but doesnt save different file only overwrites the same file and puts it
%in the data part to the left
saveas(figure(1),'fname.fig')
%below also worked but still overwriting files
savefig(figure(1),sprintf('figure%d.fig'))
%below didnt work I believe because i havent created a funtion of what
%figure%d is, like how said what psd, f,t,fs is above
savefig([ResDir,fname,'.png'], 'figure%d')
%below also didnt work
q=figure(1)
savefig([ResDir,fname,'.png'], 'q')
%below didnt work, said unrecognised function or variable for fighandles
i = 1 : length(fighandles)
filepath = fullfile(source,sprintf('specs.png',figname{i,1}))
saveas(fighandles(i), filepath)

답변 (1개)

madhan ravi
madhan ravi 2020년 6월 15일
doc sprintf % please read it more carefully
  댓글 수: 11
Rachael Courts
Rachael Courts 2020년 6월 16일
Hi Stephen and Madhan,
I appreciate the help! Stephen I tried the
for k = 1:10
fname = sprintf('im%d.png',k)
saveas(figure(1), fname)
end
The code now creates 10 images of the same file, which is overwritten by the next file in the loop?
Madhan, I tried my very best with sprintf before being sent to the link :) thanks for the help! New to this.
Rachael Courts
Rachael Courts 2020년 6월 16일
Figure it out by doing:
save([ResDir,fname,'.mat'],'PSD', 'F', 'T', 'fs')
fname % write to screen to monitor which file is being processed
%below is to save the figure
baseFileName = sprintf('im%d.png',i)
fullFileName = fullfile('E:\\Test\\', baseFileName)
saveas(figure(1), fullFileName)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scopes and Data Logging에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by