For looping figures creation

I have created code which saves a bunch of files inside a folder of my main directory.
e.g. C:\matlabstuff\Work\data
where i run all my code in Work and the data the first half of the code produces is in 'data'.
i have code which i was given by someone which starts with
[inFile,inDir]=uigetfile('*.fid','Select file');
FID=readSimpson([inDir,inFile]);
and ends with
plotSpectrum(FREQ,SPE);
shg
With proceessing and other stuff in between. I am looking to try to for loop this process so that i can select a directory as opposed to a single file, then it creates all the figures from the data and saves each figure with the names '01xxxx.fig' all the way up to ~'30xxxx.fig'
I assume this will start with something like
fids = uigetdir ('C:\matlabstuff\Work')
info = dir(fullfile(fids,'*.fid'))
list = {info.name}
which will give me the ammount of files in the directory so that i have have my for loop start as
for i = 1:length(list)
After this i am a little lost on where to go from here.
For info 'FID' is processed into 'SPE' and FREQ is generrated based on SPE.

 Accepted Answer

hello
see example code below for listing and sorting filenames in natural order (what matlab does not do well by default) in a given folder
here we load multiple excel files
hope it helps
fileDir = pwd; % current folder
outfile = 'OUT.xlsx'; % output file name
fileNames = dir(fullfile(fileDir,'data*.xlsx')); % get list of data files in directory
fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
M= length (fileNames_sorted);
out_data = [];
for f = 1:M
% option # 1 for numeric data only using importdata
raw = importdata( fullfile(fileDir, fileNames_sorted{f}));
% vertical contatenation of all individual files data
out_data = [out_data; raw.data];
end
% store out_data in excel file
writematrix(out_data,fullfile(fileDir,outfile));

6 Comments

of course , you can add your plots inside the for loop
if you need further support let me know / share your data if you want
i have manged to make some progress using the following
fids = uigetdir ('C:\matlabstuff')
info = dir(fullfile(fids,'*.fid'))
list = {info.name}
list = natsortfiles(list) %my gawd this is useful
for i = 1:length(list)
raw = importdata( fullfile(fids, list{i}));
data = raw.data;
data = data(:,1)
FID = data;
SPE=FT(FID);
plotSpectrum(gca,FREQ,SPE); %plotSpectrum is a function that basically just uses the normal plot function but sets a few things like colours and x and y max values
end
The final trouble i think i am having is that the plotting only plots one of them when in reality i would like it to plot the figure according to plotSpectrum, then to save and close the spectrum so that the next cycle of the loop can start.
ok
you need to create each figure before calling plotSpectrum
for i = 1:length(list)
raw = importdata( fullfile(fids, list{i}));
data = raw.data;
data = data(:,1)
FID = data;
SPE=FT(FID);
figure(i);
plotSpectrum(gca,FREQ,SPE); %plotSpectrum is a function that basically just uses the normal plot function but sets a few things like colours and x and y max values
end
i know you can open a figure to extract data with
fig = openfig(figfile,'invisible');
could i go the same here someway so the figures dont actually flash up on my screen?
And how do i save these figures with different names based on i?
somthing like this?
saveas (gcf, {i}xxx)
savefig ('stuff')
Having this in the loop seems to work but it just overrides each one so that i end up with only a single figure saved
do :
filename = ['stuff' num2str(i)];
savefig(filename)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!