How can I loop a code and function through different text files from the same folder?

6 views (last 30 days)
Hi I have multiple files that are named "tree_impulse_eg1.txt" and the rest of them end in _eg2, _eg3, etc.. This file gets read through a code:
file_eg1 = textread('tree_impulse_eg1.txt', '%s', 'delimiter', '\n', ...
'whitespace', ''); %reads the ADINA output text file into matlab and creates it under one ccolumn multiple rows
IndexC_eg1 = strfind(file_eg1, 'Local node 1');
Index_eg1 = find(not(cellfun('isempty', IndexC_eg1))); %looks for where there's a text that says 'Local node 1' and reads you what row it is under
foutput_eg1 = file_eg1(Index_eg1)
foutput_eg1 = strtrim(regexprep(foutput_eg1,' +',' ')); %outputs the data that is read from above into one column of "Local node 1 _____ ______ ______" 1000 times and spaces it by 1 space
save tree_impulse_eg1.mat %variable file
What this code does is reads the text file and converts it into the variable, foutput_eg1, which is a 1000x1 matrix with different character values that are differentiated by a space. Then I have a function:
function eg1 = tree_impulse_debug(foutput_eg1)
eg1 = cellfun(@(x)[{regexp(x,'Local node [0-9]+','match','once')},...
num2cell(sscanf(x,'Local node %*d%f%f%f'))'],...
foutput_eg1,'uniform',0);
eg1 = reshape([eg1{:}],4,[])';
end
and what this function does is convert the variable foutput_eg1 into a final variable, eg1, which is a 1000x4 matrix with all of the values that I need. Now I end up running the code first and then the function separately and what I need is a loop that does all of this but ends up with different matrices eg2, eg3, eg4 ... and so on, based on the initial .txt files that are named accordingly, and these .txt files are under the same folder.
Please help, and thank you!

Answers (1)

Joshua Long
Joshua Long on 7 Sep 2016
I understand that you would like to loop through the files "tree_impulse_eg1.txt", "tree_impulse_eg2.txt", and so on and perform this operation on them so that you get matrices "eg1", "eg2", etc.
Instead of naming the resulting matrices "eg1", "eg2", and so on, I recommend using a cell array. You can access "eg1" by "eg{1}".
Please refer to the following code snippet on how to process the information from "tree_impulse_eg*.txt".
number_of_files = 2; % set your number of files here
eg = cell(1, number_of_files);
%%construct
for idx = 1:number_of_files
file_num = num2str(idx);
file_eg = textread(['tree_impulse_eg' file_num '.txt'], '%s', 'delimiter', '\n', ...
'whitespace', ''); %reads the ADINA output text file into matlab and creates it under one ccolumn multiple rows
IndexC_eg = strfind(file_eg, 'Local node 1');
Index_eg = find(not(cellfun('isempty', IndexC_eg))); %looks for where there's a text that says 'Local node 1' and reads you what row it is under
foutput_eg = file_eg(Index_eg);
foutput_eg = strtrim(regexprep(foutput_eg,' +',' ')); %outputs the data that is read from above into one column of "Local node 1 _____ ______ ______" 1000 times and spaces it by 1 space
eg{idx} = tree_impulse_debug(foutput_eg);
end
  1 Comment
Ignacio Cetrangolo
Ignacio Cetrangolo on 8 Sep 2016
Thank you for looking deeply into this. I copied and pasted this code into a new file and called it "help_test.m" so I changed the second to last line to read:
eg{idx} = help_test(foutput_eg);
and when I press Run button I get the error:
Attempt to execute SCRIPT help_test as a function:
C:\Users\etc...etc\help_test.m
Error in help_test (line 12)
eg{idx} = help_test(foutput_eg);
I have a feeling that this error has to do with running it but I'm not sure how to fix it. Any help? Thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!