erasing all the rows of a cell matrix that contain a specific string variable
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sabbas
el 13 de Jul. de 2012
Editada: per isakson
el 14 de Ag. de 2014
Dear all, I have a cell matrix where its first column is
'MATA'
[ NaN]
'PANWE 1'
' CONSISTENE'
' POPULAZE'
[ NaN]
‘MATA’
'PANWE 1'
' CONSISTENE'
' POPULAZE'
[ NaN]
I want to erase all the rows that contain the word “MATA”
cheers
1 comentario
Respuesta aceptada
per isakson
el 13 de Jul. de 2012
Editada: per isakson
el 13 de Jul. de 2012
Your solution is based on an undocumented behavior of strcmp. strcmp according to the documentation should not take a double nor a cell array of double, e.g. NaN, {NaN}.
The function, find, may be removed. I guess the Code Analyzer indicates that.
A = { 'MATA'
NaN
'PANWE 1'
' CONSISTENE'
' POPULAZE'
NaN
'MATA'
'PANWE 1'
' CONSISTENE'
' POPULAZE'
NaN };
match2 = find(strcmp(A(:, 1), 'MATA'));
A(match2,:)=[];
is_MATA = strcmp(A(:, 1), 'MATA');
A( is_MATA, : ) = [];
.
--- Cont. ---
I think your solution is risky (and incorrect). You cannot complain if TMW decides to block it and throw an error, but users of your code might blame you. Read the documentation on STRCMP. It is for strings.
There need to be a good reason to use strcmp like this - IMO.
Here is a code I came up with
is_string = cellfun( @(x) ischar( x ), A, 'uni', false );
is_MATA = cellfun( @(is,x) (is && strcmp(x,'MATA')), is_string, A );
A( is_MATA, : ) = [];
It's a bit complicated.
I have used strfind with flints (floating integers). There used to be a real performance advantage over find. I called it "us' trick" and I know how to find the trick in my code.
>> strfind( ( 65:75), 67 )
ans =
3
>> strfind( ( 65:75), (67:68) )
ans =
3
4 comentarios
per isakson
el 15 de Jul. de 2012
Editada: per isakson
el 15 de Jul. de 2012
It depends on the context - the purpose of the code.
EDIT:
Thus, I have to start a discussion with Jan.
In case you anticipate that the code, which you are developing, will be used and maintained with future releases of Matlab then I would definitely recommend against using "strcmp(A(:,1),'MATA')".
On the other hand if you will use the code for a short period of time and then throw it away, why not use "strcmp(A(:,1),'MATA')".
END EDIT
However, I would not recommend that construct and it is because STRCMP is not intended for double.
What role does NaN play in the cell array, A? I would try replace NaN with a string of some kind. And why the the leading space in of some strings?
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings 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!