MATLAB Answers


MatLab classes using other classes

Asked by Lucas
on 4 Oct 2012

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 = { };
          function obj = TreeStructure(ModelName, Parent)
              if nargin == 0
                  obj.mModelName = ModelName;
                  obj.mParent = Parent;
          function [obj ID] = addnode(obj, Parent, ChildsName)
              obj.mChildren = [obj.mChildren ChildsName];
              obj.mParent = [obj.mParent Parent];
              ID = numel(obj.mChildren);

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:

      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).



No products are associated with this question.

1 Answer

Answer by per isakson
on 4 Oct 2012
Edited by per isakson
on 4 Oct 2012
 Accepted answer

Yes, instances of other classes may be values of properties.

    obj.my_property = [ OtherClass, OtherClass ]; 

creates an array of instances of OtherClass.


BTW: see Tree data structure as a MATLAB class


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!