How to store several tables in one table using a loop
    6 views (last 30 days)
  
       Show older comments
    
    Tamara Szecsey
 on 11 Nov 2020
  
    
    
    
    
    Commented: Tamara Szecsey
 on 27 Dec 2020
            I want to store several csv files into tables which I then store in one table in order to plot them easily. Storing one csv file into one table works perfectly. Matlab won't use my counter for the for-loop as table fieldname, so it overwrites the first entry. This is my code so far:
counter = 1;
filename = ['measurement' sprintf( '%03d', counter) '.csv'];
while exist(filename, 'file') == 2
    filename = ['measurement' sprintf( '%03d', counter) '.csv']
    T.counter = readtable(filename) % this isn't working
    counter = counter + 1;
end
The output is a 1x1 table T with the csv content named 'counter'. What I'd like to have is a n x 1 table T with n equals the amount of csv files. 
0 Comments
Accepted Answer
  Ameer Hamza
      
      
 on 11 Nov 2020
        
      Edited: Ameer Hamza
      
      
 on 11 Nov 2020
  
      Creating a table of tables might not be a good strategy. The usual way is to create a cell array of tables. The following code shows an example
files = dir('measurement*.csv');
T = cell(size(files));
for i = 1:numel(files)
    T{i} = readtable(files(i).name) % this isn't working
end
More Answers (0)
See Also
Categories
				Find more on Startup and Shutdown in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
