There are a number of problems with your data_class1 class.
classdef data_class1 < handle
properties (SetAccess = public, GetAccess = public)
data
samplingrate
end
Your data_class1 is a subclass of handle but is not a subclass of your myproject class. It is independent of myproject.
methods (Access = public)
function self = myproject
The comment is incorrect. The constructor for a class must have the same name as the class itself. Because this method is not called data_class1 it is a method of data_class1 named myproject (though it has a few issues that I'll describe below.) Class constructors don't need to accept an instance of the class as input. Static methods of a class don't need to accept an instance of the class as input. [In both cases they can but they are not required to.] Non-static methods must accept an instance of the class as at least one of their inputs. That's how MATLAB knows which implementation of that class method to call. [Suppose there was another class, data_class2, that defined a myproject method. Would myproject() call data_class1's myproject() or data_class2's myproject()? Actually given all your code, it would try to create a myproject() object, ignoring both the data classes.]
Your myproject method is not the constructor for this class, it's not Static, and it accepts no inputs therefore it cannot be called.
self.data = load("data_class1_data.mat")
Rather than loading this (potentially very large) data file once every time this method gets called (assuming you either make it the constructor of the data_class1 class or allow it to accept an input that isa data_class1) I'd store it as a Constant property that gets populated when the class first gets instantiated. self.samplingrate = 1e500000;
That sampling rate is not only absurd, it's infinite. It is much, much larger than realmax and so overflows to Inf.
On to your main question:
I dont know if my data_class can inheret only some of the methods from the myproject class or if I make data_class < myproject, it will inheret all the methods and properties.
You can make methods and properties have private Access to restrict their use only to methods of the class that define them, or you can restrict via method and property attributes what classes are allowed to use methods and properties of a class, but for what you're describing I would use inheritance. classdef book
properties
theText
end
methods
function read(thebook)
end
end
end
All book objects can be read and have theText.
classdef murderMystery < book
properties
theVictim
theDetective
end
properties(Hidden, Access=protected)
theMurderer
end
methods
function solve(thebook)
end
end
end
All murderMystery objects are books, and they have a theVictim, theDetective, and a theMurderer (who the audience doesn't know about, their identity is Hidden!) You can read() a murderMystery (since it isa book) or access its theText, but you can also solve() a murderMystery. Note that you can't solve() an arbitrary book, you can only solve() it if it isa murderMystery.
Make a base class (for this example, book is the base class) and store the properties and methods that all of your data objects must share in it. [Consider making it an Abstract class if it's not meant to be used on its own.] Each subclass can inherit from one or more base classes, getting those properties and methods the base class provides (read(), theText) as well as whatever properties and/or methods (if any) it provides itself (theVictim, theDetective, theMurderer, solve().)
Best Answer