Save images in for loop without overwrite

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)

Respuestas (1)

madhan ravi
madhan ravi el 15 de Jun. de 2020
doc sprintf % please read it more carefully

11 comentarios

Rachael Courts
Rachael Courts el 15 de Jun. de 2020
Hi Madhan,
As sprintf is used for 'Format data into string or character vector' this isnt something I want to be using. I saw it used on another figure loop mathworks question. At that point I wanted to try anything that could potentially work.
use sprintf
for i = 1:10
fname = sprintf('im%d.png',i)
imwrite(A, fname)
end
Rachael Courts
Rachael Courts el 16 de Jun. de 2020
Hi Darova,
Trying out your suggestion. I did try something similar earlier but couldn't get it to run because I couldn't work out what I'm to replace 'A' with. Can you please help?
Thank you!
Rachael Courts
Rachael Courts el 16 de Jun. de 2020
Aware for imwrite A = image data so I attempted 'figure (1), fname, NFFT...
Stephen23
Stephen23 el 16 de Jun. de 2020
imwrite is not suitable for printing a figure to file, it is only used for saving an image array to file.
For printing a figure use saveas.
Hi Stephen,
I was originally using the saveas:
saveas(figure(1),'test.fig')
I tried putting this in a loop and it just kept overwriting the same file.
Attempted the imwrite suggestion above and it just created 10 images of the same image?
I dont know if running a for loop, within a for loop is working?
Can you please provide some advice on how I can get the loop (above) to name the images different names so it doesnt overwrite them?
for k = 1:10
...
saveas(figure(1),sprintf('test%d.fig',d)) % you didn't read the documentation carfully , did you :0
end
Stephen23
Stephen23 el 16 de Jun. de 2020
Editada: Stephen23 el 16 de Jun. de 2020
darova already showed you how to use sprintf correctly, the only problem is imwrite is not suitable.
for i = ...
... your code
fname = sprintf('im%d.png',k);
saveas(figure(1), fname)
end
See also:
madhan ravi
madhan ravi el 16 de Jun. de 2020
Thank you Darova and Stephen ;)
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.
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)

Iniciar sesión para comentar.

Preguntada:

el 15 de Jun. de 2020

Comentada:

el 16 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by