user612610
user612610

Reputation: 19

How to write a function that accepts a cell array of strings and builds a vector of numbers of the same lenght

I'm trying to write a function that accepts a cell array of strings and builds a vector of numbers of the same length. Each item in the output vector will be the length of the strings in each cell array. If someone could please help me or show me a written example of this I would appreciate it very much. I'm new to matlab and have been working on getting this to work for a long time and cant on my own. Thank You.

Upvotes: 1

Views: 396

Answers (2)

nibot
nibot

Reputation: 14958

A slightly more succinct version of zellus's answer:

s = {'one', 'two', 'three'};
numbers = cellfun(@length, s)

Upvotes: 2

zellus
zellus

Reputation: 9582

Cellfun is one option to retrieve the string lengths as shown in the following example:

s = cellstr(strvcat('one','two','three'))
numbers = cellfun(@(x) length(char(x)), s)

Upvotes: 1

Related Questions