Calculating with Methods from Classes
Afficher commentaires plus anciens
Hi,
I'm currently getting into classes and methods, due to a project I'm working on. I have a function which needs a whole lot of other funcions to calculate it's variables.
Until now i made a Class containing all the functions I need. Now I want to give the Class the needed Properties and want it to automatically run every function in the Class and print out the final result.
I have quite a hard time finding a solution for that because I don't really now how/where to start. To begin with, I don't even know if classes are able to do what I want.
Has anybody some advice? It would be greatly appreciated!
Greetings,
Leon
2 commentaires
Adam
le 31 Mar 2020
is the best source for class-based help and information.
In terms of your question it is a bit hard to answer with so little context. Giving properties to a class is as easy as simply setting them, e.g.
myObj = MyClass( param1, param2, param3 );
or
myObj = MyClass;
myObj.param1 = param1;
myObj.param2 = 7;
etc.
And in terms of running every function in a class, there is really no different to how you put functions together outside of a class, other than that all the data is contained in the class.
You can create a function that will call the other functions in succession if that is what you want. And to print out the final result can be done however you wish. It can be an output argument of this one 'master' function of the class or you can have the class function itself do the actual printing out of it rather than returning it (which seems less useful in general).
Leon Stolp
le 31 Mar 2020
Réponses (2)
Peng Li
le 31 Mar 2020
0 votes
You have too many class methods that are not really methods specific for the class, as they never accept your object handle as an input. If they are necessary helper functions, make then out of the class def block. If they are necessarily need to be class methods, you'd better make the handle as the input, and within the function block, call explicitely the properties.
If you find it too difficult to organize so many properties (at least I do), you could probably categorize them into different groups by using struct. Your property names are too difficult to remember anyway.
3 commentaires
Leon Stolp
le 31 Mar 2020
Peng Li
le 31 Mar 2020
this looks strange as get.V supposes to get the value of V. It's weired that you make it a different thing while trying to access it. If this is a necessary calculation, it makes more sense for me to simplify it by Vnew = obj.v * .5144 whenever you want to use it. This makes the class body cleaner so that you don't necessarily need to write up a get.V function for it.
Leon Stolp
le 31 Mar 2020
Steven Lord
le 31 Mar 2020
Modifié(e) : Steven Lord
le 31 Mar 2020
To me, it seems like most if not all of these "calc<something>" methods should probably be property get methods and the properties those methods calculate should be Dependent properties. See the "Calculate Data on Demand" link in the description of the Dependent attribute on that page for more information.
But this assumes that you actually need to use a class. You don't really seem to have any methods that do something other than calculating a value. To me that suggests that a plain old function (perhaps one that operates on a struct array, to avoid requiring passing in fifteen input arguments in a specific order) would be better suited.
function HC = holstropCharacteristics(Geschwindigkeit, Deplacement, HolstropProperties)
% Define constants
g = 9.81;
vis = 1.1392e-06;
% % Unpack the properties if you want
Lwl = HolstropProperties.Lwl;
V = = HolstropProperties.V;
% and calculate with the unpacked properties
HC.Fn = calcFn(Lwl, V, g);
% Or pass the properties into the subfunctions en masse
% and let the subfunction retrieve what they need or want
HC.Rn = calcRn(HolstropProperties, vis);
% etc.
end
function Fn = calcFn(Lwl, V, g)
Fn = V/(sqrt(Lwl*g));
end
function Rn = calcRn(HP, vis)
Rn = (HP.V*HP.Lwl)/vis;
end
5 commentaires
Leon Stolp
le 31 Mar 2020
Leon Stolp
le 1 Avr 2020
Steven Lord
le 1 Avr 2020
Classes can help you avoid long function argument lists. It doesn't in this case, because your Holtrop1 function accepts a lot of inputs (but should accept one more, the object itself; unlike some other languages there's no inherent "this" input, you have to accept it explicitly.)
Another approach that can simplify and shorten argument lists is passing struct arrays into functions as I did with calcRN in the example. That has other benefits as well; in your example, I can pass the properties struct into methods and let those methods pick and choose what information they need, ignoring the rest. If I find that I need to add data to the struct I can without changing any signatures of existing functions.
Leon Stolp
le 1 Avr 2020
Leon Stolp
le 1 Avr 2020
Catégories
En savoir plus sur Structural Mechanics 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!