How can I recognize an empty row in the middle of an Excel file?
4 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!