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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by