How can I recognize an empty row in the middle of an Excel file?
4 views (last 30 days)
Show older comments
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 Comment
rees adah
on 12 Nov 2019
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?
Accepted Answer
Akira Agata
on 11 Jul 2018
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,:);
More Answers (1)
Pawel Jastrzebski
on 10 Jul 2018
Edited: 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};
2 Comments
rees adah
on 12 Nov 2019
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?
See Also
Categories
Find more on Spreadsheets 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!