MATLAB Answers

0

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

Asked by Sarah Levovitz on 10 Jul 2018
Latest activity Commented on by Sarah Levovitz on 12 Jul 2018
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!

  0 Comments

Sign in to comment.

2 Answers

Answer by Akira Agata
on 11 Jul 2018
 Accepted Answer

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 Comment

Sign in to comment.


Answer by Pawel Jastrzebski on 10 Jul 2018
Edited by Pawel Jastrzebski on 10 Jul 2018

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};

  1 Comment

Thank you for the fast reply!

Sign in to comment.