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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by