Importing multiple .txt files into one table

14 次查看(过去 30 天)
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 个评论
Aaron Waldman
Aaron Waldman 2019-2-4
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.
TADA
TADA 2019-2-4
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

请先登录,再进行评论。

采纳的回答

YT
YT 2019-2-4
编辑:YT 2019-2-9
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 个评论
Aaron Waldman
Aaron Waldman 2019-2-6
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
Aaron Waldman
Aaron Waldman 2019-2-6
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by