user5603723
user5603723

Reputation: 193

Importing a class in matlab

Pretty new to matlab. I want to have a class, which does some calculation. I want to import this class in another class( not instantiate). and use the functions as default functions.

This did not help me much. Can we import a user defined class/functions?

Upvotes: 0

Views: 210

Answers (1)

Jonas
Jonas

Reputation: 74930

So you have a class calculationClass, and you want to create another class otherClass that can access the calculations provided by calculationClass

One way that works if the calculations are either normal or static methods would be to subclass calculationClass, i.e. start your class definition with

classdef otherClass < calculationClass
[some code here]
end

This way, all methods of calculationClass immediately become available to otherClass. Note that if calculationClass has a nonempty constructor, the subclass will call the constructor as this = this@calculationClass.

If the calculations are static methods only, you can, alternatively, access those calculations as calculationClass.someCalculation(inputArguments), or create a package and use import.

Upvotes: 1

Related Questions