我有一個這樣的函式簽名:
function MyFunc(a, b, options)
%% Function argument validation
arguments
%% @Required parameters:
a (1,1) {mustBeInteger, mustBePositive}
b (1,1) {mustBeInteger, mustBePositive}
%% @Optional parameters:
options.n_bar (1,1) {mustBeInteger, mustBeLessThanOrEqual(options.n_bar, a*b*2)} % !!!Error!!!
end
% ... Function body of MyFunc goes here ...
我想options.n_bar根據aand的值添加一個約束b,例如options.n_bar <= a*b*2. 如上面的代碼片段所示,我試圖實作這一點,但 MATLAB 不允許我以這種方式做到這一點。我怎樣才能讓它作業?
uj5u.com熱心網友回復:
在引數塊中的驗證器函式呼叫中唯一允許的內容是常量運算式,如"string"or 123,或先前宣告的引數。
這闡明了輸入引數之間的依賴關系。此用例非常適合自定義驗證器功能。
function MyFunc(a, b, options)
arguments
a (1,1) {mustBeInteger, mustBePositive}
b (1,1) {mustBeInteger, mustBePositive}
options.n_bar (1,1) {myMustBeLessThanOrEqual(options.n_bar, a, b)}
end
end
function myMustBeLessThanOrEqual(w, x, y)
mustBeLessThanOrEqual(w, x*y*2);
end
上面的自定義驗證器myMustBeLessThanOrEqual包含支持的運算式,并且在自定義驗證器中可以使用任何自定義邏輯進行驗證。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432369.html
標籤:matlab
上一篇:sed命令不適用于正則運算式
