Type conversion happens before matlab Set method is called in classes, problem with setting int64(Nan) = [] default is 0
조회 수: 3 (최근 30일)
이전 댓글 표시
I am trying to set the default behaviour of a class so that int64(NaN) is set to empty and Not to the default 0.
Here is a simple class example class example:
classdef TestClass < matlab.mixin.SetGet
% CLASSDEF TestClass class to test add NaN
%
properties
a int64 {mustBeNonNan} = []
end % methods
methods
function obj = TestClass()
end % TestClass
function set.a(obj, val)
disp(val)
class(val)
obj.a = val;
end
end % methods
end % class
If you try and set a = Nan here it gets set to 0, in the set method it is already a int64 and not a double NaN. Is there a way to change this? I don't really want to addListeners and handle preSet methods. I also want the class type to be an int64 rather than not give it a type and then set the type in the set method.
On another note why is int64(NaN) set to 0 anyway?
This would work, but then the class property would not be an int64.
classdef TestClass < matlab.mixin.SetGet
% CLASSDEF TestClass class to test add NaN
%
properties
a
end % methods
methods
function obj = TestClass()
obj.a = int64([]);
end % TestClass
function set.a(obj, val)
if isnan(val), val = []; end
obj.a = int64(val);
end
end % methods
end % class
댓글 수: 4
Matt J
2022년 11월 28일
I also want the class type to be an int64 rather than not give it a type and then set the type in the set method.
I don't see why you would prefer that. One is just a shorthand for the other.
답변 (2개)
Matt J
2022년 11월 28일
One possibility:
>> obj=myclass; obj.a=1:5, obj.a(2)=nan
obj =
myclass with properties:
a: [1 2 3 4 5]
obj =
myclass with properties:
a: [1 3 4 5]
classdef myclass < matlab.mixin.SetGet
% CLASSDEF TestClass class to test add NaN
%
properties
a
end % methods
methods
function set.a(obj, val)
val(isnan(val))=[];
obj.a = int64(val);
end
function val=get.a(obj)
val=double(obj.a);
end
end % methods
end % class
댓글 수: 0
Matt J
2022년 11월 28일
Another approach, using subsasgn(). This one avoids conversion of obj.a to double.
>> obj=myclass; obj.a=1:5, obj.a(2)=nan
obj =
myclass with properties:
a: [1 2 3 4 5]
obj =
myclass with properties:
a: [1 3 4 5]
classdef myclass < matlab.mixin.SetGet
% CLASSDEF TestClass class to test add NaN
%
properties
a int64
end % methods
methods
function obj=subsasgn(obj,S,val)
obj=builtin('subsasgn',obj,S,val);
if strcmp(S(1).type,'.') && strcmp(S(1).subs,'a')
b=false(size(obj.a));
if numel(S)>1
b(S(2).subs{:})=isnan(val);
else
b=isnan(val);
end
obj.a(b)=[];
end
end
end % methods
end % class
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!