Extracting values by looping through multiple files

8 views (last 30 days)
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 Comments
dpb
dpb on 9 Jun 2019
[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
GS76 on 9 Jun 2019
Hi dpb,
Thank you for your assistance.
I have added my code in, but still not correct.
You are not the only one stating that putting sequential files into variables is a bad idea, but then how else should it be done.
Apologies! Just not clear to me.
%% Clear workspace, clear command window and close all.
clear
clc
close all
%% Looping through the *.out files.
d=dir('*.out');
for i=1:numel(d)
a = fileread(d(i).name);
% a = fileread(filename);
% 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 j = 1:length(c)
aux = (a(c(j)+586:c(j)+700 ));
d = regexp(aux,'([0-9.E-+]*)','tokens');
result = [result;table(repmat(b{j},2,1),[d{2};d{6}])];
end
Lg = 0.75;
% 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;
end

Sign in to comment.

Accepted Answer

dpb
dpb on 9 Jun 2019
Edited: dpb on 9 Jun 2019
displ=[]; % empty displacement array...
d=dir('*.out');
for i=1:numel(d)
txt=fileread(d(i).name); % read file in turn...see previous note on ordering...
txt=split(txt,{[char(13) char(10)]}); % turn into cellstr array for line indexing instead of character
% return the displacement data--this returns node as well as all directional in case may want later...
ix=find(contains(txt,'( H A R M O N I C = ')); % find the interesting section
for i = 1:length(ix)
displ=[displ; cell2mat(textscan(char(txt(ix(1)+[18:19].')).',repmat('%f',1,4)))];
end
end
% operate on resulting displacement array here...
...
  3 Comments
dpb
dpb on 9 Jun 2019
For the same reason there's one behind result in
result = [result;table(repmat(b{j},2,1),[d{2};d{6}])];
--vertical catenation.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!