code to run multiple times in for loop
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Mathieu NOE
2021년 10월 28일
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));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!