MatLab classes using other classes

11 次查看(过去 30 天)
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).

采纳的回答

per isakson
per isakson 2012-10-4
编辑:per isakson 2012-10-4
Yes, instances of other classes may be values of properties.
obj.my_property = [ OtherClass, OtherClass ];
creates an array of instances of OtherClass.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by