Is there a reason why {'charVector'} is not considered a text scalar for argument validation purposes?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Steven Lord
le 3 Juin 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}
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)
Réponse acceptée
Taylor
le 3 Juin 2024
Déplacé(e) : Matt J
le 3 Juin 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.
Plus de réponses (1)
Taylor
le 3 Juin 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 commentaires
Stephen23
le 3 Juin 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 ?
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!