Filter löschen
Filter löschen

Is there a reason why {'charVector'} is not considered a text scalar for argument validation purposes?

6 Ansichten (letzte 30 Tage)
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 Kommentare
Steven Lord
Steven Lord am 3 Jun. 2024
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 am 4 Jun. 2024
Bearbeitet: Matt J am 4 Jun. 2024
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Taylor
Taylor am 3 Jun. 2024
Verschoben: Matt J am 3 Jun. 2024
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.

Weitere Antworten (1)

Taylor
Taylor am 3 Jun. 2024
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 Kommentare
Matt J
Matt J am 3 Jun. 2024
Bearbeitet: Matt J am 3 Jun. 2024
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 am 3 Jun. 2024
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by