Clear Filters
Clear Filters

Input Argument Validation Issue

2 views (last 30 days)
Paul
Paul on 29 Apr 2024
Edited: Stephen23 on 30 Apr 2024
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 Comment
Stephen23
Stephen23 on 30 Apr 2024
Edited: Stephen23 on 30 Apr 2024
"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."

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Apr 2024
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.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!