Property Type in constructor vs dependent
I am using a function handle in a class and there seems to be a difference whether I set it in properties or in dependent properties. In the second case I need to add a cast to my property or else it doesn’t work.
There seems to be some type related thing I do not understand.
Thank you for your help!
Function
This is the function and it’s execution (in the class D_b is defined as a double):
D_b = 19;
f = @(t)(D_b/2)*(cos(t)+t.*sin(t));
x = f(linspace(0,pi/4,50)’);
Works without cast
When I define it in properties and initialize it via the constructor like below, it works:
properties(SetAccess=immutable, GetAccess=public)
% Base circle diameter
D_b double
% Involute function x axis
i_x function_handle
end
function obj = Spur()
%….
obj.D_b = 38; %Example
obj.i_x = @(t)(D_b/2)*(cos(t)+t.*sin(t));
%….
end
Fails without cast
When I try to use dependents it fails unless I cast D_b/2 to a double:
properties (Dependent)
D_b double
i_x function_handle
end
methods
function db = get.D_b(obj)
db = double(obj.D*cosd(obj.phiDegrees)); %CASTING
end
function ix = get.i_x(obj)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t));
end
end
If I don’t cast the following error occures (which is not the case with my first approach):
Error using *
Integers can only be combined with integers of the same class, or scalar doubles.
Error in Spur>@(t)(obj.D_b/2)*(cos(t)+t.*sin(t)) (line 97)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t));I am using a function handle in a class and there seems to be a difference whether I set it in properties or in dependent properties. In the second case I need to add a cast to my property or else it doesn’t work.
There seems to be some type related thing I do not understand.
Thank you for your help!
Function
This is the function and it’s execution (in the class D_b is defined as a double):
D_b = 19;
f = @(t)(D_b/2)*(cos(t)+t.*sin(t));
x = f(linspace(0,pi/4,50)’);
Works without cast
When I define it in properties and initialize it via the constructor like below, it works:
properties(SetAccess=immutable, GetAccess=public)
% Base circle diameter
D_b double
% Involute function x axis
i_x function_handle
end
function obj = Spur()
%….
obj.D_b = 38; %Example
obj.i_x = @(t)(D_b/2)*(cos(t)+t.*sin(t));
%….
end
Fails without cast
When I try to use dependents it fails unless I cast D_b/2 to a double:
properties (Dependent)
D_b double
i_x function_handle
end
methods
function db = get.D_b(obj)
db = double(obj.D*cosd(obj.phiDegrees)); %CASTING
end
function ix = get.i_x(obj)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t));
end
end
If I don’t cast the following error occures (which is not the case with my first approach):
Error using *
Integers can only be combined with integers of the same class, or scalar doubles.
Error in Spur>@(t)(obj.D_b/2)*(cos(t)+t.*sin(t)) (line 97)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t)); I am using a function handle in a class and there seems to be a difference whether I set it in properties or in dependent properties. In the second case I need to add a cast to my property or else it doesn’t work.
There seems to be some type related thing I do not understand.
Thank you for your help!
Function
This is the function and it’s execution (in the class D_b is defined as a double):
D_b = 19;
f = @(t)(D_b/2)*(cos(t)+t.*sin(t));
x = f(linspace(0,pi/4,50)’);
Works without cast
When I define it in properties and initialize it via the constructor like below, it works:
properties(SetAccess=immutable, GetAccess=public)
% Base circle diameter
D_b double
% Involute function x axis
i_x function_handle
end
function obj = Spur()
%….
obj.D_b = 38; %Example
obj.i_x = @(t)(D_b/2)*(cos(t)+t.*sin(t));
%….
end
Fails without cast
When I try to use dependents it fails unless I cast D_b/2 to a double:
properties (Dependent)
D_b double
i_x function_handle
end
methods
function db = get.D_b(obj)
db = double(obj.D*cosd(obj.phiDegrees)); %CASTING
end
function ix = get.i_x(obj)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t));
end
end
If I don’t cast the following error occures (which is not the case with my first approach):
Error using *
Integers can only be combined with integers of the same class, or scalar doubles.
Error in Spur>@(t)(obj.D_b/2)*(cos(t)+t.*sin(t)) (line 97)
ix = @(t)(obj.D_b/2)*(cos(t)+t.*sin(t)); class, properties, casting, function handle, double MATLAB Answers — New Questions