Input Argument Validation Issue
1 次查看(过去 30 天)
显示 更早的评论
I'm having an issue with input argument validation using the arguments block. Here's a simplified version of my code
function plot(obj, diagramType)
arguments
obj (1,:)
diagramType (1,:) {mustBeMember(diagramType,{"Flow Rate", "Velocity"})} = "Flow Rate";
end
end
I'm trying to override the plot command for a class of objects, but when I call this plot function, I get the following error:
Invalid default value for argument 'diagramType'. Value must be members of the required set.
This is confusing because it seems like the default value I chose - "Flow Rate" is clearly in the required set. What could this issue be?
1 个评论
Stephen23
2024-4-30
编辑:Stephen23
2024-4-30
"What could this issue be?"
This:
{"Flow Rate", "Velocity"}
Either use a string array or a cell array of character vectors, but do not create cell arrays of string scalars (which, because it is a container array of container arrays, is not processed as text). The MATLAB documentation specifically recommends avoiding them: "Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages that come from using string arrays. And in fact, most functions do not accept cell arrays of strings as input arguments, options, or values of name-value pairs. For example, if you specify a cell array of strings as an input argument, then the contains function throws an error."
采纳的回答
Walter Roberson
2024-4-29
diagramType (1,:) {mustBeMember(diagramType,["Flow Rate", "Velocity"])} = "Flow Rate";
You were using a cell array of string; you either need a plain array of string or else a cell array of character vectors.
0 个评论
更多回答(0 个)
另请参阅
类别
在 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!