For looping figures creation

조회 수: 1 (최근 30일)
Ben Smith
Ben Smith 2021년 10월 11일
댓글: Mathieu NOE 2021년 10월 11일
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.

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 10월 11일
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
Ben Smith
Ben Smith 2021년 10월 11일
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
Mathieu NOE
Mathieu NOE 2021년 10월 11일
do :
filename = ['stuff' num2str(i)];
savefig(filename)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by