Input Argument Validation Issue

8 ビュー (過去 30 日間)
Paul
Paul 2024 年 4 月 29 日
編集済み: Stephen23 2024 年 4 月 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
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
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 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by