Delete Empty Rows in a Cell Array

6 visualizaciones (últimos 30 días)
bah327hp bah327hp
bah327hp bah327hp el 26 de Jun. de 2017
Editada: bah327hp bah327hp el 3 de Jul. de 2017
Hello. Here's what I am trying to do.
1. Read in data from Excel in a cell array.
2. Delete the empty rows (i.e., rows with no data).
3. Write the array to a sheet in Excel.
So far, my code deletes the first empty row but then goes no farther.
Here is what I have based on https://stackoverflow.com/questions/31818057/deleting-empty-rows-in-a-cell-array:
[~, ~, DeleteEmptyWilliams]=xlsread('StimuliInExcel.xlsx', 'WilliamsList')
for m = 1:124
NewWilliams=DeleteEmptyWilliams(~all(cellfun('isempty',DeleteEmptyWilliams(:,1:8)), 2),:)
end
xlswrite('StimuliInExcel.xlsx',NewWilliams,'WilliamsListNoEmpty')
I have also tried the following code by adding (m,1:8), but it doesn't work either---instead, I get this error: "Index exceeds matrix dimensions". I expect this error occurred because I am deleting rows as I go, so the array's dimensions change.
[~, ~, DeleteEmptyWilliams]=xlsread('StimuliInExcel.xlsx', 'WilliamsList')
for m = 1:124
NewWilliams=DeleteEmptyWilliams(~all(cellfun('isempty',DeleteEmptyWilliams(m,1:8)), 2),:)
end
xlswrite('StimuliInExcel.xlsx',NewWilliams,'WilliamsListNoEmpty')
I tried the following code based on the suggestion at https://www.mathworks.com/matlabcentral/newsreader/view_thread/164617, but that did not work. I get this error: "Input array is empty".
[~, ~, DeleteEmptyWilliams]=xlsread('StimuliInExcel.xlsx', 'WilliamsList')
for m = 1:124
NewWilliams=DeleteEmptyWilliams(all(cellfun(@isempty,DeleteEmptyWilliams(:,1:8)), 2),:)
end
xlswrite('StimuliInExcel.xlsx',NewWilliams,'WilliamsListNoEmpty')
I would appreciate any help you can provide.

Respuesta aceptada

Image Analyst
Image Analyst el 30 de Jun. de 2017
Editada: Image Analyst el 30 de Jun. de 2017
Why not just use readtable() to read in the file, then use all() and ismissing() to remove totally empty rows?
Attach a workbook with the paperclip icon if you want a demo.
  3 comentarios
Image Analyst
Image Analyst el 3 de Jul. de 2017
Try this:
t = readtable('StimuliInExcel.xls', 'Sheet', 'WilliamsList')
% Extract columns 3-8
t = t(:, 3:8)
% Find out which entries are missing
notThere = ismissing(t)
% Find out what rows have every column as missing
badRows = all(notThere, 2)
% Extract only good rows
t = t(~badRows, :)
bah327hp bah327hp
bah327hp bah327hp el 3 de Jul. de 2017
When I use this code, it works perfectly. I really appreciate your help!
I added Name-Value pair arguments to readtable() and writetable() so that the first row of my data would not be read or written as variable names.
The entire code looks like this:
t=readtable('StimuliInExcel.xls', 'Sheet', 'WilliamsList', 'ReadVariableNames', false)
notThere=ismissing(t)
badrows=all(notThere,2)
t=t(~badrows,:)
writetable(t, 'StimuliInExcel.xls', 'Sheet', 'WilliamsListNoEmpty', 'WriteVariableNames', false)

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 26 de Jun. de 2017
Editada: dpb el 27 de Jun. de 2017
[~,~,raw]=xlsread('StimuliInExcel.xlsx', 'WilliamsList');
raw=raw(~all(cellfun(isempty,raw),2),:);
xlswrite('StimuliInExcel.xlsx',raw,'WilliamsListNoEmpty')
  14 comentarios
bah327hp bah327hp
bah327hp bah327hp el 3 de Jul. de 2017
If you visually inspect the Excel file, the CompleteList sheet has no missing values, but the WilliamsList and PalmerList sheets do. Image Analyst provided a great answer.
bah327hp bah327hp
bah327hp bah327hp el 3 de Jul. de 2017
Editada: bah327hp bah327hp el 3 de Jul. de 2017
Thank you for all your help and the information you provided!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by