Delete rows in cell that contain part of string

17 visualizaciones (últimos 30 días)
newbie9
newbie9 el 13 de Mzo. de 2019
Comentada: Image Analyst el 17 de Jul. de 2024
I have an Nx2 cell (mixes of strings and doubles) that I'm trying to clean up (rawdata). Specifically, I am trying to delete all rows with ">" in the first column. My code below is deleting those rows, but it is reshaping the cell as an Mx1 cell (data1). I'm not sure where the error is. Thanks for any help.
rawdata = textscan(fid, '%s %s' , 'HeaderLines', 7);
rawdata = [rawdata{:}];
data1 = rawdata(cellfun(@(s)isempty(regexp(s,'>')),rawdata));

Respuesta aceptada

madhan ravi
madhan ravi el 13 de Mzo. de 2019
Editada: madhan ravi el 13 de Mzo. de 2019
Cell(strcmp(Cell(:,1),'>'),:)=[]
  3 comentarios
KAE
KAE el 17 de Jul. de 2024
So compact!
Image Analyst
Image Analyst el 17 de Jul. de 2024
Example with 2-by-2 cell array:
Cell = { '>', 123, 'abc', 'xyz'; '2abc', 456, '>' 'str'}
Cell = 2x4 cell array
{'>' } {[123]} {'abc'} {'xyz'} {'2abc'} {[456]} {'>' } {'str'}
Cell(strcmp(Cell(:,1),'>'),:)=[]
Cell = 1x4 cell array
{'2abc'} {[456]} {'>'} {'str'}
Note that row 1 is gone because the first cell in that row is '>'.
Not sure how certain you are that the > contains no white space but you may make it more robust by using strtrim and/or startsWith, for example if the > was the first non-white character and had other characters after it, you could do it this way:
Cell = { ' > ', 123, 'abc', 'xyz'; '2abc', 456, '>' 'str'}
Cell = 2x4 cell array
{' > '} {[123]} {'abc'} {'xyz'} {'2abc' } {[456]} {'>' } {'str'}
Cell(startsWith(strtrim(Cell(:,1)),'>'),:)=[]
Cell = 1x4 cell array
{'2abc'} {[456]} {'>'} {'str'}

Iniciar sesión para comentar.

Más respuestas (1)

newbie9
newbie9 el 13 de Mzo. de 2019
Editada: newbie9 el 13 de Mzo. de 2019
Below works, but perhaps is not the most elegant code.
rawdata = textscan(fid, '%s %s' , 'HeaderLines', 7);
fclose all;
rawdata = [rawdata{:}];
idx = strfind(rawdata(:,1),'>');
tf = cellfun('isempty', idx);
tf = [tf tf];
data1 = rawdata(tf);
data1 = reshape(data1,[length(data1)/2, 2]);

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by