curvefit.fitoptions的属性类验证

问题描述 投票:1回答:1

TL; DR:我想使用property class validation验证fitoptions对象,但MATLAB抱怨。


请考虑以下类:

fitoptions

我想强制子类为% SUPERCLASS: classdef (Abstract) BaseModel % CWLvsT is an abstract parent class for different CWL(T) models. properties (Abstract = true, Access = protected, Constant = true) MODEL (1,1) string FITOPTS (1,1) % ??? end end % SUBCLASS: classdef RationalFraction < BaseModel % This corresponds to a fit of type "rat11". properties (Access = protected, Constant = true) MODEL = "CWL(T) = (P1 * T + P2) / (T + Q1)"; FITOPTS = fitoptions('rat11', ... 'Lower', [-1, -1E5, -1E4], ... 'Upper', [10, 5E5, 5E4], ... 'StartPoint', [4, 0, 0]); end end 属性指定某种类型的fitoptions对象(有几种类型)。

尝试#1

我试图做的第一件事是创建一个示例FITOPTS对象(根据子类中的定义),然后查看其fitoptions是什么。我得到的结果是class,所以我尝试将它而不是curvefit.nlsqoptions放在超类中并实例化% ???类,但是出现以下错误:

RationalFraction
尝试#2

[我认为上述错误表明Error defining property 'FITOPTS' of class 'BaseModel': Class named 'curvefit.nlsqoptions' is undefined or does not support property validation. 有一些超类,我应该针对该超类来验证对象,因此我尝试对示例对象进行fitoptions,但得到了空的metaclass(optsObj)。依靠手动搜索,我确实通过遍历metaclass文件夹-MATLAB\R2019b\toolbox\curvefit\curvefit\@curvefit找到了一个不错的候选人,但这导致了类似的错误。

尝试#3

我注意到curvefit.basefitoptions对象与普通fitoptions对象相距不远,所以我尝试了struct-这[[did成功创建了对象。不幸的是,当稍后在尝试调用FITOPTS (1,1) struct时引用此FITOPTS字段时,出现以下错误:

fit
这意味着将属性验证

cast

Error using fit>iParseOptionalArgs (line 949) Algorithm options must be specified in a FITOPTIONS object or as property-value pairs. Error in fit (line 113) [useroptions, useroptargs, probparams] = iParseOptionalArgs( varargin{:} ); 对象转换为nlsqoptions,从而丢失了它保留适合选项的“额外信息”。尽管此行为是一个有趣的发现,但在当前情况下不是很有用。我的问题:[有人可以建议一种执行此类验证的方法,以使struct最终包含适合选项对象吗?

我正在使用R2019b。

matlab validation oop properties curve-fitting
1个回答
2
投票
出现在问题中的最后一个错误是指FITOPTS中的第949行。如果我们查看此文件,则会看到“候选”适合选项对象被传递给名为fit的函数,该函数检查其是否有效。

这暗示了验证输入的另一种机制-将它们传递给isfitoptions。尽管这些函数用于验证

value,而不是class,但没有理由不能将它们也用于后者。因此,如果用户可以调用MATLAB的函数,则可以将其用于验证,如果不能,则可以在我们自己的类中将同一测试(对validation functions的简单调用)实现为方法。幸运的是,此功能is可在包/工具箱外部访问,以下内容可产生所需的结果:

isa
© www.soinside.com 2019 - 2024. All rights reserved.