Navid
Navid

Reputation: 29

How to find a string within a cell array

I have a cell array like this:

'10 Hz: Time_abs'
<1x2 cell>
<1x2 cell>
<1x2 cell>
<1x2 cell>
<1x2 cell>
<1x2 cell>
<1x2 cell>
'10 Hz: Time_abs'
<1x2 cell>
<1x2 cell>
<1x2 cell>

At first I need to find in which rows there is 10 Hz: Time_abs and then delete the corresponding row. I cannot use strcmp because the other rows are <1x2 cell>.

Can anybody help me with that?

Upvotes: 0

Views: 84

Answers (2)

Clemens
Clemens

Reputation: 245

In one line:

C{1,1} = magic(5);
C{1,2} = 'John Dump';
C{1,3} = 1 + 1i     
C{1,4} = 0.0025

x = cellfun(@ischar, C)

x =

     0     1     0    0

Upvotes: 0

NKN
NKN

Reputation: 6424

You can iterate cell by cell and check for being string using isstr function, for instance:

A{1} = 'sdadfadf';
A{2} = 23;
A{3} = [1,2,3,4];
A{4} = 0;


for ii=1:length(A)
isstr(A{ii})
end

ans =  1
ans = 0
ans = 0
ans = 0

The other solution is using ischar function:

C{1,1} = magic(5);
C{1,2} = 'John Dump';
C{1,3} = 1 + 1i     
C{1,4} = 0.0025

for k = 1:4
x(k) = ischar(C{1,k});
end

x

x =

     0     1     0    0

Upvotes: 2

Related Questions