Is there a reason why {'charVector'} is not considered a text scalar for argument validation purposes?
조회 수: 13 (최근 30일)
이전 댓글 표시
It seems like a software design inconsistency to me that this passes validation,
mustBeText({'charVector'})
mustBeScalarOrEmpty({'charVector'})
but this does not,
mustBeTextScalar({'charVector'})
Is there a logic to this?
댓글 수: 2
Steven Lord
2024년 6월 3일
It probably could be clearer, but I suspect one problem that the developers were trying to avoid: is C a text scalar?
S = ['ab';'cd'];
C = {S}
isscalar(C)
iscellstr(C)
C is a scalar and it is a cellstr, and so you could argue it is a text scalar. But the text it contains isn't really one piece of text. To me that "feels like" two pieces of text data (stored as a char matrix, not a char vector) that happens to be stored in one cell of a cell array. We could make the description of what mustBeTextScalar does more complicated (it has to be a scalar string or a row char vector or a scalar cell whose one cell contains a scalar string or a row char vector) but that's starting to get harder to parse (for a human, not for MATLAB!) And then if we define "a scalar cell whose one cell contains a scalar string or a row char vector" as a text scalar, does a cell array containing that count as a text scalar?
C2 = {{'hello'}} % text scalar or no?
Lest you think that an artificial example, well it is since I wrote it up myself. But if you have space padded data (so that two pieces of text can be concatenated together) it looks a little more realistic:
C3 = {['apple '; 'banana']}
celldisp(C3)
iscellstr(C3)
isscalar(C3)
채택된 답변
Taylor
2024년 6월 3일
이동: Matt J
2024년 6월 3일
Apologies, I should have said " A cell array is not necessarily interpretted as "text" ". I think I have a more complete understanding of the situation now. mustBeTextScalar should operate like a logical evaluation of "text" AND "scalar" given that mustBeText and mustBeTextScalar return "true" for a cell containing a character array. I have reported this inconsistency to the development team.
추가 답변 (1개)
Taylor
2024년 6월 3일
From the mustBeText documention "mustBeText(value) throws an error if value is not a string array, character vector, or cell array of character vectors". mustBeScalarOrEmpty returns nothing because by enclsoing the character array in curly brackets, it becomes a 1x1 cell. From the mustBeTextScalar documentation "mustBeTextScalar(value) throws an error if value is not a text scalar". The 1x1 cell is scalar but not text hence the error.
댓글 수: 7
Stephen23
2024년 6월 3일
Another another another way to ask this:
C = {'a','bb','ccc'}; % if this is
mustBeText(C) % text
X = C(1); % then why is this not
mustBeTextScalar(X) % scalar text ?
참고 항목
카테고리
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!