count nonblank values

10 ビュー (過去 30 日間)
Patrick
Patrick 2011 年 1 月 29 日
Hi, Can anyone tell me how to count nonblank values in an array? I want to find something like:
if number of nonblank values in A > 5 do something
else move on to next record
Thanks Patrick

採用された回答

Andrew Newell
Andrew Newell 2011 年 1 月 29 日
By blank, do you mean blank characters in a string S? If so, you could do this to count the nonblank characters:
num_nonblank = sum(S~=' ');
Or, if S is a cell array of strings,
num_nonblank = sum(~strcmp(S(:),''));
  1 件のコメント
Patrick
Patrick 2011 年 1 月 29 日
Sorry ,I sould have said string. Thanks, your answer works. Patrick

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

その他の回答 (1 件)

Jan
Jan 2011 年 1 月 29 日
If "blanks" means all white space characters like spaces, line breaks, tabs, formfeeds etc:
num_non_blank = sum(~isspace(S));
Equivalent:
num_non_blank = sum(S <= char(32));
The ASCII-code of all control characters and the space are lower than 33. I'm not sure about the effects of multi-byte characters, e.g. Unicode file names.
  1 件のコメント
David
David 2013 年 7 月 2 日
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 want a one or two-line solution 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 place in the matrix.
David Horton

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by