How to delete a row with specific letter

i have a mixed data (1000 by 6) at which the first column contains letter like N, K , L and O. How can i delete a rows which contain letter 'K'. Thanks

 Accepted Answer

Jan
Jan on 7 Jul 2015
Edited: Jan on 7 Jul 2015
If the "(1000 by 6)" data are a CHAR Matrix:
A(A(:,1)=='K') = [];
But "mixed data" could mean, that you have a cell array. So perhaps you want:
A(strncmp(A(:, 1), 'K', 1), :) = [];

7 Comments

thanks. The following error message appears
Undefined function 'eq' for input arguments of type 'cell' .
the data looks like
'L' [] [] [] [] []
'K' [] [] [] [] []
'N' [] [] [] [] []
.
.
.
So you see it is important to explain exactly, what kind of data you are talking of. See the strncmp approach.
Do you mean something like this:
Data = {'L', [], [], [], [], [], []; ...
'K', [], [], [], [], [], []}
? Please post always the Matlab code, which re-creates the array, you are talking of. Then the readers do not have to guess.
To import to matlab i used
*[T,M,D]= xlsread('data.xlsx)*
so
D= 'L', [], [], [], [], [], []; ...
'K', [], [], [], [], [], [] without the *{}*
my objective is to remove rows which contain letter K from d . Thank
Thank you very much!!!
strncmp works
dear Jan Simon,
i am working with similar data with a bit complex one. Since my contains o,oh and ho .I want to remove row start with oh . For that i used d(strncmp (d(:, 1), 'oh', 1), :) = []; . The problem is it removed also rows start with o. is ther a way to solve this problem. Thank you in advance.
@Abebe kebede: read the documentation for strncmp. It checks as many characters as the last argument tells it to, which is your case is one, so both 'oh' and 'o' match this. Change the value to two, and you will find that it works:
strncmp(d(:, 1), 'oh', 2)
Dear Stephen Cobeldick
Thank you so much. i see my mistake

Sign in to comment.

More Answers (1)

A(ismember(A(:,1),'K'),:)==[]

6 Comments

Thanks! but i get the following error: Error using cell/ismember>cellismemberR2012a (line 199) Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.
my code loos like the following
[T,M,D]= xlsread('data.xlsx); D(ismember (D(:,1),'K'),:)==[];
Try
A(ismember(A(:,1),{'K'}),:)==[]
thanks!
the same error message
Sorry it s
A(ismember(A(:,1),'K'),:)=[]
@Azzi: There is no need for ismember. The "==" is a typo.
Thanks so much!!
But still the same error message

Sign in to comment.

Categories

Asked:

on 7 Jul 2015

Edited:

on 8 Jul 2015

Community Treasure Hunt

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

Start Hunting!