user6110593
user6110593

Reputation:

How to delete a substring within a string

I have a string like this

a={ {'a', 'b', 'c','d'},{''}, {'e', 'f', 'g', 'h'},{''} }

where the two '' are empty {1x1 cell} substrings within the string. How do I delete empty substring like this and end up with

a={ {'a', 'b', 'c','d'}, {'e', 'f', 'g', 'h'} }

Upvotes: 1

Views: 124

Answers (2)

Cedric Zoppolo
Cedric Zoppolo

Reputation: 4743

You can try out this code to perform what you want:

a={ {'a', 'b', 'c','d'},{''}, {'e', 'f', 'g', 'h'},{''} }
j = 1
for i = 1:length(a)
   if ~ ismember( a{i}, '' )
       b{j}=a{i}
       j = j+1
   end
end

Then variable a would look like:

>> a{1}

ans = 

    'a'    'b'    'c'    'd'

>> a{2}

ans = 

    'e'    'f'    'g'    'h'

Upvotes: 0

gnovice
gnovice

Reputation: 125854

You can compare each cell of the outer cell array to {''} using cellfun and isequal, then use that as a logical index to remove those cells:

a(cellfun(@(c) isequal(c, {''}), a)) = [];

Upvotes: 3

Related Questions