Deleting Empty Rows in Cell Array

Hello,
I have a cell array like
' '
' '
'1231231'
' '
' '
'1231231'
' '
I want to delete this blank elements and transform this cell array from 7x1 to 2x1. How can I do it? Is there any methods to solve this problem without cellfun or etc.? Thank you.

 Respuesta aceptada

If the data is in a cell array, mostly likely cellfun() is needed, unless you use for-loop.
strtrim() or strip() can remove the white spaces.
b={' '
' '
'1231231'
' '
' '
'1231231'
' '};
c=strip(b);
index=cellfun(@isempty,c);
d=b(~index)
d = 2×1 cell array
{'1231231'} {'1231231'}

7 comentarios

tinkyminky93
tinkyminky93 el 6 de Jun. de 2022
Editada: tinkyminky93 el 6 de Jun. de 2022
First argument must be text, error using strip.
Jan
Jan el 6 de Jun. de 2022
@tinkyminky: Does this mean, that your cell array does not contains CHAR vectors? Then the question is misleading. Please post some code, which creates your input data.
tinkyminky93
tinkyminky93 el 6 de Jun. de 2022
Yes it is. It does not contain anything. It is just created because of the for loop and there is no data inside of it.
Jan
Jan el 6 de Jun. de 2022
@tinkyminky93: I do not understand, what you want to say. What does not contain anything? Which loop? What is the meaning of the ' ' blocks in your question?
If tzhese blocks are empty matrices instead of char vectors containing spaces, simple omit the strip command:
c = c(~cellfun('isempty',c))
tinkyminky93
tinkyminky93 el 7 de Jun. de 2022
Actually i am scanning all the text file with for loop and if it contains the data that I want, I am taking these values and deleting rest. Thats where for loop comes from.By the way my array is char array for now, is there any other method?
tinkyminky93
tinkyminky93 el 7 de Jun. de 2022
@Fangjun Jiang sir I applied your suggestion and it seems like that is what I want. But I have a problem. My data becomes 32x1 cell and every element is 2x1 cell. What I want is 32x1 and every element is 1x2. How can I do it?
Fangjun Jiang
Fangjun Jiang el 7 de Jun. de 2022
Provide some example data that others can use directly in MATLAB to help you. For example
' '
'1231231'
can not be used directly in MATLAB, while
a={' '
'1231231'}
can so others can use it to provide you with example code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2021b

Preguntada:

el 6 de Jun. de 2022

Comentada:

el 7 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by