Identify index of cell based on stringlength

I have a cell array of such:
TStr =
1×1 cell array
{2×1 cell}
TStr{1}(1) =
1×1 cell array
{'Hello... It me, I was wondering if after all these years youd like to meet'}
TStr{1}(2) =
1×1 cell array
{'1800'}
I know that
strlength(TStr{1}(2))
= 4
How do I use this information to identify the index of the cell (in this case, index = 2) with stringlength of 4?

 Accepted Answer

Example data:
Tstr{1}{1,1} = 'Hello... It me, I was wondering if after all these years youd like to meet';
Tstr{1}{2,1} = '1800';
You can get the index by:
idx = cellfun(@numel,Tstr{1})==4;
Which gives:
Tstr{1}{idx}
ans = '1800'

4 Comments

Thanks! This is plenty of help!
If you dont mind I tagging along a more specific question...
When I run this code, I get
idx =
2×1 logical array
0
1
How do I make it return 2 (the location of the cell in the cell array that has 4 stringlength)?
Pass idx into the find function
The first is a logical index, the second is a linear index but both are indicating the position.
Oh I see! Thank you very much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!