Input Argument Validation Issue
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 30 Avr 2024
Modifié(e) : Stephen23
le 30 Avr 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."
Réponse acceptée
Walter Roberson
le 29 Avr 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.
0 commentaires
Plus de réponses (0)
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!