How to load multiple files and calculate value for each file (with same formula)
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
In case that I have the separate 123 mat files (bearing1_1_1.mat to bearing 1_1_123.mat) and I would like to calculate RMS and Crest Factor of each file (i.e., RMS for bearing1_1_1 = xxxxx, CF for bearing1_1_1 = xxxxx). How to write the code?
PS. I already have code for RMS and CF calculation but I only want how to calculate them (all 123 file) in one execution.
Because now I have to do it manually file by file and it consums lot of times (i.e., run bearing1_1_1.mat and go to next data)
Thanks for your kind supports
답변 (1개)
Mathieu NOE
2022년 4월 25일
hello
this is the code (principle) to load all the mat files in a loop
% use the structure returned by DIR:
P = cd; % working directory
S = dir(fullfile(P, 'bearing*.mat'));
for k = 1:numel(S)
S(k).folder % fyi
S(k).name % fyi
F = fullfile(S(k).folder, S(k).name);
%S(k).data = load(F); % or READTABLE or whatever.
S(k).data = load(F);
end
% % structure S: it contains all of your file data and the corresponding filenames
% % For example, the 2nd filename and its data:
% S(2).name
% S(2).data
댓글 수: 1
Mathieu NOE
2022년 4월 25일
maybe you want to have the files sorted correctly and matlab is not very good for that
so in case the files are not processed in the "right" sorted manner , you may need this excellent FEX submission :
the code is now :
% use the structure returned by DIR:
P = cd; % working directory
% S = dir(fullfile(P, 'bearing*.mat'));
S = dir(fullfile(P, 'Data*.mat'));
S = natsortfiles(S); % sort folders / file names into natural order
% (download FEX : https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:numel(S)
S(k).folder % fyi
S(k).name % fyi
F = fullfile(S(k).folder, S(k).name);
%S(k).data = load(F); % or READTABLE or whatever.
S(k).data = load(F);
end
% % structure S: it contains all of your file data and the corresponding filenames
% % For example, the 2nd filename and its data:
% S(2).name
% S(2).data
참고 항목
카테고리
Help Center 및 File Exchange에서 Vibration Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!