フィルターのクリア

Get lengths of nonuniform-length character strings in an array without counting padded blanks

1 回表示 (過去 30 日間)
Isn't there an easy way to get the lengths of non-blank parts of character vectors placed into an array like this:
A = char('Small', 'Medium', 'Very Large')
The answer I'd like is: 5 6 10. I'd like a one or two-line solution if possible that returns a vector with the lengths of the non-blank parts of each character string in the matrix but counts blanks that fall WITHIN the character string [between words.] In the above case, the matrix A would be 3x10 but I want a function that ignores only the blanks padded onto the end of each string when the matrix is formed and I can't always get the length of the vectors before they're placed in the matrix.
David Horton

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 2 日
out=cellfun(@numel,cellstr(A))
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 2 日
編集済み: Azzi Abdelmalek 2013 年 7 月 2 日
You can also use arrayfun:
out=arrayfun(@(x) numel(strtrim(A(x,:))),1:size(A,1))
% look at strtrim function
doc strtrim
David
David 2013 年 7 月 2 日
Thanks again - very informative.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 7 月 2 日
cellfun(@(S) find(S~=' ',1,'last'), A)
  1 件のコメント
David
David 2013 年 7 月 2 日
Thanks but this doesn't work on a character array and I have tried arrayfun, which functions similarly to cellfun. Perhaps using arrayfun is a solution but I haven't figured out yet HOW to us it appropriately.
Thanks for your help anyway.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by