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
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2013년 7월 2일
out=cellfun(@numel,cellstr(A))
댓글 수: 3
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
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!