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!