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'})
Value must be a character vector or string scalar.
Is there a logic to this?
  댓글 수: 2
Steven Lord
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}
C = 1x1 cell array
{2x2 char}
isscalar(C)
ans = logical
1
iscellstr(C)
ans = logical
1
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?
C2 = 1x1 cell array
{1x1 cell}
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']}
C3 = 1x1 cell array
{2x6 char}
celldisp(C3)
C3{1} = apple banana
iscellstr(C3)
ans = logical
1
isscalar(C3)
ans = logical
1
Matt J
Matt J 2024년 6월 4일
편집: Matt J 2024년 6월 4일
C is a scalar and it is a cellstr, and so you could argue it is a text scalar.
That is food for thought, Steve, but to my mind cell arrays of char vectors exclusively are meant to be interpretted as "text", and not more general variables satisfying iscellstr(). Since mustBeText() doesn't even recognize your example as text, that seems to be where the developers were going:
mustBeText( {['ab';'cd']} )
Value must be a character vector, string array, or cell array of character vectors.

댓글을 달려면 로그인하십시오.

채택된 답변

Taylor
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
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
Matt J
Matt J 2024년 6월 3일
편집: Matt J 2024년 6월 3일
Perhaps another way to ask it is this. Consider the following variables.
T1={'a','bb','ccc'};
T2=string(T1);
I cannot think of any text-manipulating Matlab function (replace(), contains(),startsWith(), etc...) that does not accept either T1 or T2 as input interchangeably. So why wouldn't both T1 and T2 be considered "text"?
Stephen23
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 ?
Value must be a character vector or string scalar.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by