MatLab classes using other classes

10 views (last 30 days)
I’m new to using MatLabs oop and I got a simple tree structure going right now.
classdef TreeStructure < handle
%TREESTRUCTURE Summary of this class goes here
% Detailed explanation goes here
properties (SetAccess = private)
mModelName = '';
mParent = [ ];
mChildren = { };
end
methods
function obj = TreeStructure(ModelName, Parent)
if nargin == 0
else
obj.mModelName = ModelName;
obj.mParent = Parent;
end
end
function [obj ID] = addnode(obj, Parent, ChildsName)
obj.mChildren = [obj.mChildren ChildsName];
obj.mParent = [obj.mParent Parent];
ID = numel(obj.mChildren);
End
end
end
You can call it like:
tree = TreeStructure('Top', 0);
[tree ID] = addnode(tree, 0, 'obj1');
[tree ID] = addnode(tree, ID, 'obj2');
[tree ID] = addnode(tree, 1, 'obj3');
[tree ID] = addnode(tree, ID, 'obj4');
I have some other functions that’ll parse the tree and it does what I want, but I want to add some features to it. Right now when you run the last thing you get a result that looks like:
Properties:
mSubsystemName: 'Top'
mCurrentPath: ''
mParent: [0 0 1 1 3]
mChildren: {'obj1' 'obj2' 'obj3' 'obj4'}
I’m going to be using this on Simulink models and I’m going to want more info than the subsystems name. I want to create another class, let’s call it node, that’ll contain more info on the contents of the subsystem. Is it possible to have a structure or cell of another class? I’d like mChildren to contain node classes that are filled with things we decide to fill it with.
I seen that you can call other classes from within a class, the bank account demo, but I never seen them used as data types(I think that’s what I mean).

Accepted Answer

per isakson
per isakson on 4 Oct 2012
Edited: per isakson on 4 Oct 2012
Yes, instances of other classes may be values of properties.
obj.my_property = [ OtherClass, OtherClass ];
creates an array of instances of OtherClass.
.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!