function r = plus(p,q)
r.prop = p;
r.prop = q;
obj1 = myclass(struct('field1',,'field2',));
obj2 = myclass(struct('field1',,'field2',));
r = myclass(add(obj1.prop, obj2.prop));
this is my code. my structure array consists of fields:field 1 & field2. my class has a property: prop. I have made functions which uses struct and I want to use these functions under the class methods. How do I go about defining in the class that I will be using struct in the whole class?
It looks to me like you're trying to use the constructor to initialise
prop with a structure. Then you're using the
plus method to add a new structure to
prop as an array item. If this is what you're trying to do you could do it like this:
classdef myClass < handle properties prop end methods function obj = myClass(p) obj.prop = p; end function addItem(obj, q) obj.prop = [obj.prop, q]; end end end
You can use this like this:
p = struct('field1',,'field2',); x = myClass(p); x.addItem(p);
x.prop would be:
1x2 struct array with fields: field1 field2
This class does not force the input values to be structs. You could do some checking and throw an error if the input value isn't of the required format.
You can define the type of your properties in the properties definition block.
classdef myclass properties prop = struct end end
If you then try to write another type to prop, e.g. a double value with
obj.prop = 2 in the constructor, there will be an error.