How to change a certain array cell depending on its length

Hello,
I currently have this very simple code:
if strlength(A) >= 250
A = [];
end
My objective is for every cell that A has which is larger than 250 characters, make that specific array row 0.
e.g. ( The 12th row of array A has more than 250 characters in it, thus it should become 0 after the code runs)
However, my problem is that the program just skips over the if statement as if it's false when in reality I have characters which go beyond 250.
I tried implementing the cellfun into this as well just in case every row of the array isn't being checked.
if cellfun(@strlength, A)>=250
A = [];
end
but still the same problem.
I know the if statement isn't false because upon running just
strlength(A) >= 250
I have a whole list of 0's (which corespond to characters under 250) and some 1's (which are those above 250)

 Respuesta aceptada

Adam Danz
Adam Danz el 19 de Sept. de 2019
Editada: Adam Danz el 19 de Sept. de 2019
If 'A' is a cell array of character vectors and you'd like to replace cells that have a length greater than 250 with an empty char array,
A(strlength(A)>250) = {''};
For string arrays (ie, ["abc", "def", "ghi" ...],
A(strlength(A)>250) = "";

8 comentarios

Nice use of string() facilities.
Nom
Nom el 19 de Sept. de 2019
Editada: Nom el 19 de Sept. de 2019
Adam you've done it again, it works!
Right before you posted the answer, I was messing around with using what you used.
But I was doing it this way instead
A(strlength(A)>250) = '';
But this ended up just deleting those with a character length larger than 250 lol.
All of this was gotten through here for anyone who wants to know:
Thanks again Adam!
Thanks & thanks!
@Nommah, if you were working with string arrays, you could use that line of code (I updated my answer to provide an example of that, too).
Nom
Nom el 19 de Sept. de 2019
Editada: Nom el 19 de Sept. de 2019
Thanks! I'm just working with a cell full of characters.
I was thinking of making this more advanced, though I'm not sure if it's possible without getting really complicated.
This is what I have so far below:
A(strlength(A)>250) = strrep(A,'C:\Desktop\','');
So in essence I would want to find the cells whose character length is greater than 250 and then apply a string replace which looks inside of A to find the string "C:\Desktop\" and replace that with just nothing (so deleting it).
However, I get the error of:
In an assignment A(:) = B, the number of elements in A and B must be the same.
I'm not sure if it's because I'm mixing up my string functions with the character arrays I'm working with.
Adam Danz
Adam Danz el 19 de Sept. de 2019
Editada: Adam Danz el 19 de Sept. de 2019
A(strlength(A)>250) = strrep(A(strlength(A)>250),'C:\Desktop\','');
Nom
Nom el 19 de Sept. de 2019
You're actually a genius, I don't know how you know so much about Matlab!
Solving everyone of my problems.
If A is a cell array of paths and you're trying to eliminate the paths and keep the filenames, this would be a better approach:
[~,filenames] = cellfun(@fileparts,A,'UniformOutput',false)
Nom
Nom el 19 de Sept. de 2019
Editada: Nom el 19 de Sept. de 2019
I've decided to use your first method, just because I understand it much more clearly.
Main reason I'm doing this is because excel has a 255 column width limit which gives an error if I try query a file whose path is more than 255 characters. By just removing the file path's header (which just removes where the file was searched for) I can still retain the important information of where the file is located.
Thank you so much though Adam,
I really really appreciate all the help you provide.

Iniciar sesión para comentar.

Más respuestas (1)

Fabio Freschi
Fabio Freschi el 19 de Sept. de 2019
Editada: Fabio Freschi el 19 de Sept. de 2019
mask = strlength(A) > 250;
A(mask) = cellfun(@(i) '', A(mask), 'uniform', 0);

1 comentario

Thank you for your answer Sir,
I've decided to use
A(strlength(A)>250) = {''};
As it's just one simple line of code which acomplishes exactly what I needed it to do.
Thank you so much though!

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

Nom
el 19 de Sept. de 2019

Comentada:

Nom
el 19 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by