Importing multiple .txt files into one table
    21 views (last 30 days)
  
       Show older comments
    
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
  TADA
      
 on 4 Feb 2019
				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
Accepted Answer
  YT
      
 on 4 Feb 2019
        
      Edited: YT
      
 on 9 Feb 2019
  
      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
More Answers (0)
See Also
Categories
				Find more on Spreadsheets in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

