MATLAB Answers

0

Importing multiple .txt files into one table

Asked by Aaron Waldman on 4 Feb 2019
Latest activity Edited by YT
on 9 Feb 2019
Hello, I have an issue I have been stuck on for a while. I have several .txt files that I want to import into matlab and produce a single table that I can export into an excel spreadsheet. I want to write a for loop that will select a range of files and then take the same data from each one which will then translate into severeal different variables onto a single table. I am very new to Matlab and any help would be much appreciated.

  3 Comments

YT
on 4 Feb 2019
Would be usefull if you attach a sample of the txt files you want to import.
Attached are a couple of txt files, I need the test names in one column and the following data in the rest. The txt file is tab delimited if that helps.
Actually, none of these files are tab delimited, nor with any other delimiter for that matter.
You probably need to parse them using a different method

Sign in to comment.

1 Answer

Answer by YT
on 4 Feb 2019
Edited by YT
on 9 Feb 2019
 Accepted Answer

This should do the trick. I've added several comments in the hope that it makes it easier for you to understand how it works. Ofcourse you won't understand everything directly when you just start out using Matlab, but I suggest to just look up the stuff you don't understand in the documentation.
clear;
%%%
aof = 4; %amount of files (M1-{number}.txt)
%%%
%creating temporary table for the parameters
tableNames = table('Size',[17 1],'VariableTypes',{'string'},'VariableNames',{'paramNames'});
tableTypes = cell(1, aof); tableTypes(:) = {'double'};
%creating temporary table for values
t = num2cell(1:aof);
tableTypes = cell(1, aof);
tableTypes(:) = {'double'};
newTable = table('Size', [17 aof],'VariableTypes',tableTypes,'VariableNames',strcat('Subject_',cellstr(cellfun(@num2str,t(:)))));
%loop for reading in files
for ii = 1:aof
fileName = ['M1-' num2str(aof) '.txt']; % < noticed this error, should be num2str(ii) instead of aof
if (ii == 1)
opts = detectImportOptions(fileName,'Delimiter','\n');
opts.DataLines = [26 63]; % extract from row 26 to 63
end
%readin table
T = readtable(fileName,opts);
%add names to tableNames
if (ii == 1)
varName = T{1:17,:};
tableNames(:,1) = varName;
end
%add values to newTable
varValue = cellfun(@str2double,T{22:38,:});
newTable(:,ii) = array2table(varValue);
end
%merge tables and save comma seperated file
finalTable = [tableNames newTable];
writetable(finalTable,'my_output_file.txt','Delimiter',',');
p.s. it's not my most beautiful creation but it works ;)
EDIT
The following should work with multiple files now. So let's say I have the files M1-1.txt, M1-2.txt, M2-1.txt and M2-2.txt. Now it will save to a final table of 17-by-5, where the first column consists of the parameter names, the second column is M1-1.txt data, third column M1-2.txt, fourth column M2-1.txt and fifth column M2-2.txt. Hope this was the answer you were looking for.
clear;
aof = 2; %amount of files; M1-{filenumber}.txt
aom = 2; %amount of M's; M{m-number}-1.txt
tableNames = table('Size',[17 1],'VariableTypes',{'string'},'VariableNames',{'paramNames'});
tableTypes = cell(1, aof); tableTypes(:) = {'double'};
%loop over Ms
for hh = 1:aom
%table subject names
t = num2cell(1:aof);
tableTypes = cell(1, aof);
tableTypes(:) = {'double'};
newTable = table('Size', [17 aof],'VariableTypes',tableTypes,'VariableNames',strcat('Subject_',num2str(hh),'_',cellstr(cellfun(@num2str,t(:)))));
%loop over files
for ii = 1:aof
fileName = ['M' num2str(hh) '-' num2str(ii) '.txt'];
if (hh == 1 && ii == 1)
opts = detectImportOptions(fileName,'Delimiter','\n');
opts.DataLines = [26 63]; % extract from row 26 to 63
end
%readin table
T = readtable(fileName,opts);
%add names to table
if (hh == 1 && ii == 1)
varName = T{1:17,:};
tableNames(:,1) = varName;
end
%add values to another table
varValue = cellfun(@str2double,T{22:38,:});
newTable(:,ii) = array2table(varValue);
end
if(hh == 1)
%merge tables
finalTable = [tableNames newTable];
else
finalTable = [finalTable newTable];
end
end
writetable(finalTable,'my_output_file.txt','Delimiter',',');

  4 Comments

Show 1 older comment
YT
on 6 Feb 2019
Yep if they all look like M1-{file_number}.txt, you just change the value of aof to the amount of files you have. So if you got 100 files, you'll put aof = 100;
So the files are M1-1-M1-5 then M2-1-M2-5, M3-1-M3-5, and M4-1-M4-5, i have been tweaking with it how to edit it so it will continue through the rest
Ok so I am thinking i can just repeat the for loop for M2, M3, and M4. but first the output text file doesnt output separate data from each file it just takes the data from the last file read and only outputs that data in each of the columns

Sign in to comment.