How to make user-defined type pass as numeric?
2 ビュー (過去 30 日間)
古いコメントを表示
Suppose I have a defined type that behaves like a number. I would like it to pass the test:
validateattributes(x, {'numeric'}, {})
It's not enough, apparently, to overload the function isnumeric.m. What else needs to be done?
Thanks, n
0 件のコメント
採用された回答
Geoff Hayes
2015 年 10 月 10 日
編集済み: Geoff Hayes
2015 年 10 月 10 日
Naor - try overloading the isa function instead. For example, let's suppose that the following class is "numeric".
classdef MyNumberClass
properties
x
end
methods
function obj = MyNumberClass(varargin)
if nargin > 0
obj.x = 42;
else
obj.x = 99;
end
end
function [result] = isa(obj,typeStr)
result = false;
if strcmpi(typeStr,'numeric') || strcmpi(typestr,'MyNumberClass')
result = true;
end
end
end
end
Then in the Command Window, try the following
myInstance = MyNumberClass;
Now try to validate it as
validateattributes(myInstance, {'numeric'}, {})
No error is thrown and so the object is considered to be numeric. Note that you would need to overload isnumeric so that that function "passes" too.
2 件のコメント
Geoff Hayes
2015 年 10 月 10 日
Naor - I've updated the solution with your suggestion. As for including your class in the error output, I'm not sure how that can be done.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Construct and Work with Object Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!