Make several tables from one table
    9 views (last 30 days)
  
       Show older comments
    
    Dion Theunissen
 on 28 May 2021
  
    
    
    
    
    Edited: Scott MacKenzie
      
 on 28 May 2021
            Hi,
I have a table like this:

Now i want to make several single tables with data out of this. I want to cut rows from IGNITION_ON until IGNITION_OFF.
How can I take those rows out of the table and save this in a seperate table?
Dion
0 Comments
Accepted Answer
  Scott MacKenzie
      
 on 28 May 2021
        
      Edited: Scott MacKenzie
      
 on 28 May 2021
  
      Try this...
% table containing your data
T = readtable('yourdata.xlxs');
% vectors of indices for ON/OFF rows
idxFirst = find(strcmp(T{:,2}, 'IGNITION_ON'));
idxLast = find(strcmp(T{:,2}, 'IGNITION_OFF'));
% create tables with data of interest and save in files Tnew1.xlsx, Tnew2.xlsx, etc.
for i=1:length(idxFirst)    
    Tnew{i} = T(idxFirst(i):idxLast(i),:); 
    writetable(Tnew{i}, sprintf('Tnew%d.xlsx', i));
end
0 Comments
More Answers (1)
  Asmit Singh
      
 on 28 May 2021
        You can extract rows from a table uising row indexing. You can have a look at the "Index Using Numeric Indices" section in the documentation.
2 Comments
  Asmit Singh
      
 on 28 May 2021
				You can initialise an emty cell array and store tables. After the for loop you will have a cell array of tables.
c = {}
%change for loop to your liking
for i = 1:5
    %replace table with your table variable
    c{i} = table
end
See Also
Categories
				Find more on Cell Arrays 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!

