Azzera filtri
Azzera filtri

Input Argument Validation Issue

4 visualizzazioni (ultimi 30 giorni)
Paul
Paul il 29 Apr 2024
Modificato: Stephen23 il 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 Commento
Stephen23
Stephen23 il 30 Apr 2024
Modificato: Stephen23 il 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."

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 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.

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by