Make several tables from one table

조회 수: 13 (최근 30일)
Dion Theunissen
Dion Theunissen 2021년 5월 28일
편집: Scott MacKenzie 2021년 5월 28일
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

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 28일
편집: Scott MacKenzie 2021년 5월 28일
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

추가 답변 (1개)

Asmit Singh
Asmit Singh 2021년 5월 28일
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
Dion Theunissen
Dion Theunissen 2021년 5월 28일
Thanks, can you maybe also help me to save all diferent tables? Now i read all seperate tables in a for loop and it overwrites each time. So on they end I only have the last table. How can I rename each table so I can save them all?
Asmit Singh
Asmit Singh 2021년 5월 28일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by