Extracting values by looping through multiple files
이전 댓글 표시
Hi all,
I have attached a zipped file containing 3 output files from a finite element analysis software. I would need to process more than 100 hundred of these files. Numerical values are extracted from the output files to calculate "A" and "B" coefficients. I have managed to get this to work for one file at a time, but I want to do this for multiple files in a routine and thus have a complete list of "A" and "B" coefficients. The formatting of the files is identical.
I am new to programming and Matlab. I have been reading about vectorization and for looping in Matlab, but I am getting confused of how to solve this problem. My apologies.
Any assistance would be much appreciated.
%% Clear workspace, clear command window and close all.
clear
clc
close all
%% Extracting *.out files.
fileinfo = dir('*.out');
fnames = sort({fileinfo.name});
[F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,...
F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,F34,F35,F36] = ...
fnames{1:36};
%% Use the above created variables in a list to read into the next section.
f = F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,
F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,F34,F35,F3;
%% Looping through the files to extract values.
a = fileread(f);
Lg = 0.75;
% find ( H A R M O N I C = \d )
[b,c] = regexp(a,'( H A R M O N I C = \d )','tokens');
result = table();
% find the 2nd and the 6th number in the table
for i = 1:length(c)
aux = (a(c(i)+586:c(i)+700 ));
d = regexp(aux,'([0-9.E-+]*)','tokens');
result = [result;table(repmat(b{i},2,1),[d{2};d{6}])];
end
% Converting strings to numbers
result.Var2 = str2double(result.Var2);
% Determine "A" cumulative strain functions
A = ((result{2,2}-result{1,2})/Lg)/2.e-6;
% Determine "B" cumulative strain functions
B = ((result{4,2}-result{3,2})/Lg)/2.e-6;
댓글 수: 2
dpb
2019년 6월 9일
[F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,...
F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,F34,F35,F36] = ...
fnames{1:36};
%% Use the above created variables in a list to read into the next section.
f = F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,
F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,F34,F35,F3;
%% Looping through the files to extract values.
a = fileread(f);
You've already got the file names in the fileinfo directory struct and will generally be sorted anyway...altho if they are named as sequential files as you've made variables (a really bad idea in Matlab) the sorting will return them in alphanumeric order, not in the numeric order you'd prefer; ie, F1,F10,F11,...F19, will precede F2,F20,F21, ...
Is it mandatory to process in a given sequence? If so, you'd be best served to rename to use a sequencing number scheme with leading zeros as F001,F002, ... F099,F100,...F999
%% Extracting *.out files.
d=dir('*.out');
for i=1:numel(d)
txt=fileread(d(i).name);
...
end
GS76
2019년 6월 9일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!