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 lengt开发者_运维问答h. 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.
A slightly more succinct version of zellus's answer:
s = {'one', 'two', 'three'};
numbers = cellfun(@length, s)
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)
精彩评论