How can I recognize an empty row in the middle of an Excel file?

조회 수: 7 (최근 30일)
Sarah Levovitz
Sarah Levovitz 2018년 7월 10일
댓글: rees adah 2019년 11월 12일
I have a program that collects two sets of data and puts them into one Excel file. The program puts the two sets of data on top of each other, with an empty row between them. Is there a way that I can separate the two sets of data into separate matrices? Can MATLAB detect the empty space and put everything before the space into one matrix and everything after the space into another?
Alternatively, for the first set of data, the first column has the word 'left' and for the second set of data, the first column has the word 'right'. Can I separate the two sets of data by the word in the first column? I'm attaching the file so you can see what I'm talking about. Thank you!
  댓글 수: 1
rees adah
rees adah 2019년 11월 12일
I had a similar problem but mine had multiple empty rows in between and I'd want to separate them into different matrix for further processing.how do I do that so it's not constrained only to the excel file I have?

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

채택된 답변

Akira Agata
Akira Agata 2018년 7월 11일
How about the following way? The Solution-1 and -2 returns the same result.
T = readtable('BF1.xlsx');
% Solution-1: Separate by detecting the empty row
pt = find(ismissing(T.Sensor));
T1 = T(1:pt-1,:);
T2 = T(pt+1:end,:);
% Solution-2: Separate by grouping 1st column
idx1 = strcmp(T.Sensor,'left');
idx2 = strcmp(T.Sensor,'right');
T1 = T(idx1,:);
T2 = T(idx2,:);

추가 답변 (1개)

Pawel Jastrzebski
Pawel Jastrzebski 2018년 7월 10일
편집: Pawel Jastrzebski 2018년 7월 10일
Consider the following code:
% STEP 1: LOAD DATA
% load excel file to a table
t = readtable('BF1.xlsx');
% this will tell you that the 'Sensor' column
% was imported as a cell:
%
% class(t.Sensor)
% STEP 2: remove empty row
% find the empty cell in the 'Sensor' column
% this create a logical vector
EmptyCell = cellfun(@isempty,t.Sensor);
% invert the logical vector and use it to create
% a new table that has all the rows but the empty one
tNew = t(~EmptyCell,:);
% STEP 3: make some changes to the table
% for efficiency, change the colmun type from:
% 'cell' to 'categorical'
class(tNew.Sensor)
tNew.Sensor = categorical(tNew.Sensor);
class(tNew.Sensor)
% STEP 4: sperate data
% data separation and stored as matrices
% if you want to keep them as tables change:
% brackets from { } to ( )
% and
% range from '2:end' to ':'
mLeft = tNew{tNew.Sensor == 'left' ,2:end};
mRight = tNew{tNew.Sensor == 'right',2:end};
  댓글 수: 2
Sarah Levovitz
Sarah Levovitz 2018년 7월 12일
Thank you for the fast reply!
rees adah
rees adah 2019년 11월 12일
I had a similar problem but mine had multiple empty rows in between data and I'd want to separate them into different matrices depending on the empty rows which would serve as separators for further processing.how do I do that so it's not constrained only to the excel file I have?

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by