manage a list of instances of a class

7 views (last 30 days)
hi everybody, i got some classes and for depending one of antoher or basicly there is one big class that contains the the other classes as properties. i woult like to write a function that manages me the
classdef node<handle
properties
x
y
bearing=0
end % properties
methods
function K = node(x,y)
% if nargin == 0, return, end
K.x=x;
K.y=y;
end%functions
end%methods
end %class
in the main should be a variable nodelist that keepst track of all the instances of node. a new one ist created like this
nodelist = {};nodelist ={nodelist{:},node(1,2)}
i tried to write a function that can create and delete a node
function nodelist=addnode(nodelist,x,y)
if exist(nodelist)==0
nodelist={};
end
nodelist = {nodelist{:},node(x,y)};
end
>>addnode(nodelist,1,2)% should be called like this
but this doesnt work and i dont know why and i send a copy of nodelist to the function wich is probably a not very economic way because the could bee 1000 of nodes in a structure. how do i use a pointer in order to make it faster?
thanks a lot in advance

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 14 Mar 2012
You might want to use a PERSISTENT variable within nodelist() to keep track of the nodes. For example:
function varargout = addnode(varargin)
% Create a persistent variable to keep track of the nodelist
persistent nodelist;
if isempty(nodelist)
nodelist = {};
end
% Add a node if given inputs
if nargin
nodelist{end+1} = node(varargin{1},varargin{2});
end
% Return nodelist if requested
if nargout
varargout{1} = nodelist;
end
This function lets you get, set, or get/set nodes:
>> addnode(1,2);
>> addnode(2,4);
>> nodes = addnode
nodes =
[1x1 node] [1x1 node]
This may be a feasible lightweaght approach for your problem, but I'd also suggest thinking about the interface you want and considering something like a NodeList class with methods like addNode() and getNode(). That would enable you to completely hide the list data structure.
  1 Comment
jeff wu
jeff wu on 20 Mar 2012
thanks a lot,
i thought about that too but i guess its gonna be a bit complicated in that case because i got the classes : beam, node, form, material and points, has all of them as properties
evrey beam has exactly 2 nodes, 1 material and 1 form, but it can have any given number of point. so i would need a nodelist, beamlist,meteriallist and a formlist. and snce every beam can have any given number of points i would need for every instance of beam a pointlist.
i hope you can follow me. i really dont know yet how to organize the programm

Sign in to comment.

More Answers (1)

Daniel Shub
Daniel Shub on 14 Mar 2012
Your node list seems to me to be related to the doubly linked list example: http://www.mathworks.co.uk/help/techdoc/matlab_oop/f2-86100.html#brikpee
As for pointers, MATLAB does not need pointers since its memory management is pretty dang smart. Basically, all "variables" in MATLAB are pointers, MATLAB keeps track of all the references to a variable and only allocates new memory if you change a variable that has another reference to it. For example, look at the memeroy usage when you do:
x = randn(1e8, 1); % Big jump
y = x; % No change
y(1)=0; % Big jump
  2 Comments
jeff wu
jeff wu on 20 Mar 2012
thanks a lot,
i thought about that too but i guess its gonna be a bit complicated in that case because i got the classes : beam, node, form, material and points, has all of them as properties
evrey beam has exactly 2 nodes, 1 material and 1 form, but it can have any given number of point. so i would need a nodelist, beamlist,meteriallist and a formlist. and snce every beam can have any given number of points i would need for every instance of beam a pointlist.
i hope you can follow me. i really dont know yet how to organize the programm
Daniel Shub
Daniel Shub on 20 Mar 2012
You have accepted an answer and posted an identical comment to both answers. It is unclear if you question is answered or if there is a new question ...
Also, it is better to convey "thanks a lot" by voting for questions and answers which is done by clicking the triangle near the avatar.

Sign in to comment.

Categories

Find more on Argument Definitions 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!