manage a list of instances of a class
Show older comments
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
More Answers (1)
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
on 20 Mar 2012
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.
Categories
Find more on MATLAB 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!