code to run multiple times in for loop

1 visualización (últimos 30 días)
Suresh R
Suresh R el 28 de Oct. de 2021
Comentada: Mathieu NOE el 5 de Nov. de 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{:})

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 28 de Oct. de 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));

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by