code to run multiple times in for loop

2 Ansichten (letzte 30 Tage)
Suresh R
Suresh R am 28 Okt. 2021
Kommentiert: Mathieu NOE am 5 Nov. 2021
i want to run the following code in for loop for n number of time say for eg 100. i have around 100 mat files in the folder.
filename='path name'
str=whos('-file',filename)
str={str.name}
load(filename,str{:})

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 28 Okt. 2021
hello
I guess you want something like that... here it's for laoding excel file but you can easily adapt it for mat files
if you want to load in natural sorting , you need also to retrieve natsortfiles from FEX :
clc
clearvars
fileDir = pwd;
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 natural 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));

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by