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)
回答 (1 件)
madhan ravi
2020 年 6 月 15 日
doc sprintf % please read it more carefully
11 件のコメント
Rachael Courts
2020 年 6 月 15 日
darova
2020 年 6 月 16 日
use sprintf
for i = 1:10
fname = sprintf('im%d.png',i)
imwrite(A, fname)
end
Rachael Courts
2020 年 6 月 16 日
Rachael Courts
2020 年 6 月 16 日
Stephen23
2020 年 6 月 16 日
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.
Rachael Courts
2020 年 6 月 16 日
madhan ravi
2020 年 6 月 16 日
for k = 1:10
...
saveas(figure(1),sprintf('test%d.fig',d)) % you didn't read the documentation carfully , did you :0
end
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
2020 年 6 月 16 日
Thank you Darova and Stephen ;)
Rachael Courts
2020 年 6 月 16 日
Rachael Courts
2020 年 6 月 16 日
カテゴリ
ヘルプ センター および File Exchange で Scopes and Data Logging についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!