remove all strings from nested cell array

Hi,
I'm trying to remove all string/chars from a 4x10 cell array where each cell in the array contains a 40x1 column vector (so as you can see, it's nested). Most commands I usually use such as cell2mat don't work on this because the cell array is nested.
Or, if preferred, is there a way to read in all the elements of a certain column in one of the cells in a cell array apart from the elements containing strings? e.g:
col=rand(4,10) %matrix containing column indices
for i=1:4
for j=1:10
new_cell_arrray{i,j}=g{i,1}(:,col(i,j)); %g is another (existing) 4x1 cell array and new_cell_array is 4x10.
end
end
Thank you

1 Comment

If you "remove" an entry from a 2D array, the result cannot be a 2D array any more.

Sign in to comment.

 Accepted Answer

C = num2cell(randi(9,4,10));
C(randperm(numel(C), 4)) = {"string"};
C(randperm(numel(C), 4)) = {'chars'};
C
C = 4×10 cell array
{[5]} {[ 8]} {[5]} {[6]} {[1]} {[ 8]} {'chars'} {'chars' } {["string"]} {'chars'} {[3]} {["string"]} {[4]} {[1]} {[2]} {["string"]} {[ 1]} {[ 9]} {[ 5]} {[ 8]} {[8]} {[ 5]} {[4]} {[8]} {[9]} {[ 2]} {[ 1]} {[ 5]} {[ 6]} {[ 7]} {[3]} {[ 3]} {[5]} {[1]} {[5]} {[ 2]} {'chars'} {["string"]} {[ 9]} {[ 8]}
for K = 1 : size(C,2)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C(:,K));
subset{K} = C(mask, K);
end
subset
subset = 1×10 cell array
{4×1 cell} {3×1 cell} {4×1 cell} {4×1 cell} {4×1 cell} {3×1 cell} {2×1 cell} {2×1 cell} {3×1 cell} {3×1 cell}
subset{1}, subset{2}
ans = 4×1 cell array
{[5]} {[3]} {[8]} {[3]}
ans = 3×1 cell array
{[8]} {[5]} {[3]}

5 Comments

Thank you so much for your response, it was very helpful! Can i ask how this could be modified in a case where each cell in the 4x10 array contains a 1x40 array that has both chars and numbers, rather than just chars only. I'm looking to eliminate just the char parts of the cells, rather than the entire cells themselves.
for J = 1 : 4
for K = 1 : 10
thiscell = num2cell(randi(9, 40, 1));
if rand() > 0.3; thiscell{randi(40)} = 'characters'; end
if rand() > 0.3; thiscell{randi(40)} = "string"; end
C{J,K} = thiscell;
end
end
whos C
Name Size Bytes Class Attributes C 4x10 187802 cell
C{1}
ans = 40×1 cell array
{[ 7]} {[ 5]} {[ 2]} {[ 7]} {[ 9]} {["string" ]} {[ 8]} {[ 2]} {[ 1]} {'characters'} {[ 7]} {[ 8]} {[ 5]} {[ 5]} {[ 7]} {[ 8]}
subset = cell(size(C));
for J = 1 : numel(C)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C{J});
subset{J} = C{J}(mask);
end
whos subset
Name Size Bytes Class Attributes subset 4x10 177088 cell
subset{1}
ans = 38×1 cell array
{[7]} {[5]} {[2]} {[7]} {[9]} {[8]} {[2]} {[1]} {[7]} {[8]} {[5]} {[5]} {[7]} {[8]} {[4]} {[1]}
Thanks again for your help, it is much appreciated! the code is giving me an empty cell array for subset. In each cell, I have 4 strings and 36 numbers. I think this is why I'm getting an empty array, because each cell contains at least one string? I would paste my code but the amount of info I'm allowed to ask is what I've posted on this forum, unfortunately I'm not allowed to post the code itself as it is my coursework :(
"In each cell, I have 4 strings and 36 numbers."
Okay, I will generate 4 strings and 36 numbers per cell.
for J = 1 : 4
for K = 1 : 10
thiscell = num2cell(randi(9, 40, 1));
rp = randperm(40,4);
thiscell(rp) = {"string"};
C{J,K} = thiscell;
end
end
whos C
Name Size Bytes Class Attributes C 4x10 206080 cell
C{1}
ans = 40×1 cell array
{[ 4]} {[ 1]} {[ 4]} {[ 4]} {[ 1]} {[ 3]} {[ 1]} {[ 6]} {[ 4]} {["string"]} {[ 4]} {[ 7]} {["string"]} {[ 1]} {[ 3]} {[ 5]}
subset = cell(size(C));
for J = 1 : numel(C)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C{J});
subset{J} = C{J}(mask);
end
whos subset
Name Size Bytes Class Attributes subset 4x10 165440 cell
subset{1}
ans = 36×1 cell array
{[4]} {[1]} {[4]} {[4]} {[1]} {[3]} {[1]} {[6]} {[4]} {[4]} {[7]} {[1]} {[3]} {[5]} {[5]} {[5]}
and see that after the filtering, there are 36 entries left. All of the string() objects and character vectors have been removed.
Thank you so much! it worked :D

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!