code to run multiple times in for loop
2 views (last 30 days)
Show older comments
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{:})
0 Comments
Accepted Answer
Mathieu NOE
on 28 Oct 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));
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!