I have a question related to cell arrays. How do I delete a specific row by checking the first element of that row?

1 view (last 30 days)
Here I have a matrix, and I want to eliminate the rows which start with '$', please go through the excel file that I have attached.
What I'm expecting is this as ouput, which I may write it to another excel file:
'c' '1' '2.400000000000000E+00' '2.400000000000000E+00' '2.400000000000000E+00' 'c' '2' '3.360000000000000E+00' '2.400000000000000E+00' '2.400000000000000E+00' 'c' '3' '4.320000000000000E+00' '2.400000000000000E+00' '2.400000000000000E+00' 'c' '4' '5.280000000000001E+00' '2.400000000000000E+00' '2.400000000000000E+00' 'c' '5' '6.240000000000000E+00' '2.400000000000000E+00' '2.400000000000000E+00'
  4 Comments
Gurubasav Yellur
Gurubasav Yellur on 7 Nov 2014
Dear Swapnil,
Thank you very much for that block of code! It works fine. I also wanted to completely delete the 6th and 7th columns. In fact, without explicitly mentioning these particular column numbers I want to eliminate all those columns which contain 'NAN'.
Swapnil
Swapnil on 7 Nov 2014
Edited: Swapnil on 7 Nov 2014
Hi, I think this will work for you.
[~,~,Cells] = xlsread('trialdata.xlsx');
Cells(find(~cellfun('isempty',strfind(Cells(:,1),'$'))),:) = [];
indexes = [];
for idx = 1:size(Cells,2)
a = cellfun(@isnan,Cells(:,idx),'UniformOutput',false);
if( all(cellfun(@any,a)))
indexes = [indexes,idx];
end
end
Cells(:,indexes) = [];

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 7 Nov 2014
To remove elements from a matrix and cell arrays you assign [] to them.
To find strings that match you use strcmp. You only want to do the comparison with the first column, so you use the () indexing for that.
Therefore:
c = {'$', '$', 'wmesh', '[geo_Meshio.c]', '--';
'$', 'c', '216', '', '';
'$', 'r', '6', '', '';
'$', 'x', '119', '' '';
'$', '$ Vertices', '', '', '';
'c', '1', '2.400000000000000E+00', '2.400000000000000E+00', '2.400000000000000E+00';
'c', '2', '3.360000000000000E+00', '2.400000000000000E+00', '2.400000000000000E+00';
'c', '3', '4.320000000000000E+00', '2.400000000000000E+00', '2.400000000000000E+00';
'c', '4', '5.280000000000001E+00', '2.400000000000000E+00', '2.400000000000000E+00';
'c', '5', '6.240000000000000E+00', '2.400000000000000E+00', '2.400000000000000E+00'};
c(strcmp(c(:, 1), '$'), :) = []
  2 Comments
Gurubasav Yellur
Gurubasav Yellur on 7 Nov 2014
Your code works perfect and that's what I am looking for, but I am not able to get the same result when I am importing that excel file data in a variable A= {}.
Here is what I tried:
A={}; A = importdata('trialdata.xlsx') A(strcmp(A(:, 1), '$'), :) = []
---------------------------------------------------
P.S You overlooked the two double quotes that are in the 6th and 7th columns respectively.
Guillaume
Guillaume on 7 Nov 2014
Right, to import your excel file, use the 2nd output of xlsread (or even the 3rd output).
[~, c] = xlsread('trialdata.xlsx');
Optionally, remove the quotes surrounding each elements:
c = cellfun(@(s) strrep(s, '''', ''), c, 'UniformOutput', false);
You can then remove the rows that start with '$' (if you don't remove the quotes, it's '''$'''):
c(strcmp(c(:, 1), '$'), :) = [];
And to remove empty columns:
c(:, all(strcmp(c, ''))) = [];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!