Property Type in constructor vs dependent
Afficher commentaires plus anciens
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));
3 commentaires
Mario Malic
le 22 Mai 2024
It is a little bit complicated to comment on your code as you are speaking about two issues (or more): one is about casting the output of multiplication of two object properties (which I don't see in your class), and the other one is the method Spur, which apparently works, but, does it? I would suggest to go over OOP basics in MATLAB, there is Onramp and video tutorial by Loren Shure available
Note that my OOP knowledge is a bit limited, so I didn't put this into answer section, but if it helps you, someone will move it.
So, you have error related to this method:
function obj = Spur()
%....
obj.D_b = 38; %Example
obj.i_x = @(t)(D_b/2)*(cos(t)+t.*sin(t));
%....
end
What does this do? If you are trying to modify the value of properties of your class, this will not do it, since you haven't supplied the object (instance of class) to the method. This creates a new structure, with fields D_b, and i_x and returns the structure as an output.
If you supplied the object to it, you would probably get an error, because you have set your class properties to be immutable. This means, that properties are set while object is constructed and afterwards, they cannot be changed.
About the casting issue
function db = get.D_b(obj)
db = double(obj.D*cosd(obj.phiDegrees)); %CASTING
end
I see no problems here, however, I don't see these properties mentioned in your class, so I assume this is different class whose information is missing. You can also inspect values of these object properties by adding the lines above the line with issue or debugging, maybe you forgot to initialize their values.
Adam
le 22 Mai 2024
Spur() is the constructor so this kind of property initialisation works fine. If obj hasn't yet been referenced when you assign to a property in the constructor it will create it at that moment.
For the rest, I'm a little confused what the actual problem is, what is working and what isn't, from the example given.
TK
le 22 Mai 2024
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Function Handles dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!