Can't configure predifined char arguments in a function

1 回表示 (過去 30 日間)
Jaime Gonzalez Gomez
Jaime Gonzalez Gomez 2020 年 12 月 11 日
コメント済み: Ameer Hamza 2020 年 12 月 11 日
I'm trying to create a function to work with structures, specifically to write information in any field.
I know I could use "structure.fieldA.fielB = data" but for the final application I'm looking for a cleaner code. That's why I'm doing this function.
Its declaration and arguments definition is the next:
function [s] = add2element(s, element, field, data)
arguments
s
element {mustBeInteger} = 1
field {mustBeText} = 'Points'
data {mustBeFinite} = 0
end
% Here next is the body of the function, not important right now
end
But when I try to call the function the following error raises:
>> example_s = add2element(example_s, 1, 'Points', 5)
Unrecognized function or variable 'mustBeText'.
Error in add2element
I've written the arguments block by looking at the Argument Validation Functions page and I don't understand why I get this error. The following are the tests I've done with no results:
  • I've tried with both validations 'mustBeText' and 'mustBeTextScalar'.
  • The field 'Points' was written in a variable field_name = 'Points'

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 11 日
編集済み: Ameer Hamza 2020 年 12 月 11 日
mustBeText and mustBeTextScalar were both introduced in R2020b; you cannot use them in R2020a. MATLAB does not provide any validation function for text validation in R2020a: https://www.mathworks.com/help/releases/R2020a/matlab/matlab_prog/argument-validation-functions.html. You can define your own validation function
function myValidationFun(x)
if ~ischar(x)
error('Not a char.');
end
end
place it on MATLAB's path and then in your argument list, specify this validation function for field like this
field {myValidationFun} = 'Points'
  2 件のコメント
Jaime Gonzalez Gomez
Jaime Gonzalez Gomez 2020 年 12 月 11 日
Ok, that explains it
Thanks a lot
Ameer Hamza
Ameer Hamza 2020 年 12 月 11 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by