"isfield" for hidden field in optimoptions

4 次查看(过去 30 天)
hi,
with Matlab 2019 it seems that:
options = optimoptions(@fsolve,'JacobPattern',speye(5))
isfield(options,'JacobPattern')
will return 0. But options.JacobPattern will return the speye(5).
I guess this is due to the hidden nature of JacobPattern. I want the code to check if the field is specified or not.
How do I do that?

采纳的回答

Walter Roberson
Walter Roberson 2025-2-11
编辑:Walter Roberson 2025-2-11
optimoptions returns on object of class optim.options.Fsolve not a struct.
You need to use
isprop(options,'JacobPattern')
However, more likely is that you want
isprop(options,'JacobPattern') && ~strcmp(options.JacobPattern), 'sparse(ones(Jrows,Jcols))')
  3 个评论
Walter Roberson
Walter Roberson 2025-2-11
I dunno. It is documented though
If S is not a structure array, then isfield returns 0.
I guess it makes it marginally easier to handle the situation where a function returns either a structure array or else []
Stephen23
Stephen23 2025-2-11
Such situations are also easily handled by the user: isstruct(S) && isfield(S,..)

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2025-2-11
编辑:Matt J 2025-2-11
One way:
options = optimoptions(@fsolve,'JacobPattern',speye(5));
isNonDefault = ~isequal(options.JacobPattern, optimoptions(@fsolve).JacobPattern )
isNonDefault = logical
1

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by