try to manage a dynamic list of classinstances

조회 수: 4 (최근 30일)
jeff wu
jeff wu 2012년 3월 24일
hi its me again, i figured a lot out already thanks to your help, but there are still some misteries. i got this:
classdef node<handle
properties
x;
y;
bearing=0;
end % properties
methods
function obj = node(x,y)
obj.x=x;
obj.y=y;
end%functions
end%methods
end %class
and this:
classdef manlists
properties
nodelist={}
stablist={}
materiallist={}
formlist={}
end
methods
function f = manlists()
f.nodelist={node(1,2);node(2,3)};% as a try, and here works the node instance
f.stablist={};
f.materiallist={};
f.formlist={} ;
end
function h = grN(obj)
h = size(obj.nodelist,1);
end
function h = AddNode(a,b)
h.nodelist{end+1} =node(a,b)
end
end
end
but it doesnt work like i expect it to
in the command window it works
>> man = manlists;
>> man.nodelist{end+1}=node(1,2)
man =
manlists
Properties:
nodelist: {3x1 cell}
stablist: {}
materiallist: {}
formlist: {}
Methods
but inside the class i get this error:
>> man.AddNode(1,2)
??? Error using ==> AddNode
Too many input arguments.
i tried arround for several hours but i cant figure it out please help
thanks
  댓글 수: 3
Daniel Shub
Daniel Shub 2012년 3월 25일
It is also strange to me that nodelist is a cell array instead of an array of class node.
per isakson
per isakson 2012년 3월 25일
Why do you use cell arrays and not ordinary arrays?

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Shub
Daniel Shub 2012년 3월 25일
The call
man.AddNode(1,2)
is converted behind the scenes to
AddNode(man, 1, 2)
which has three arguments, but AddNode only takes 2 arguments. What I think you "want" is
function h = AddNode(h,a,b)
  댓글 수: 1
jeff wu
jeff wu 2012년 3월 26일
thanx a lot,
it works now in the way man = manlists; man = man.Addnode(1,2);
but is there a way to add a node without assigning man again;
actually i thought and what i wanted to do is that by calling man.Addnode(1,2) that one would automatically be added to the nodelist without deleting the previous.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2012년 3월 25일
Try
man = manlists;
man.nodelist(end+1) = node(1,2);
man = man.AddNode(1,2);
man.grN
with these two classes
classdef node < handle
properties
x;
y;
bearing=0;
end % properties
methods
function this = node(x,y)
this.x=x;
this.y=y;
end%functions
end%methods
end %class
classdef manlists
properties
nodelist = node.empty
end
methods
function this = manlists()
this.nodelist(end+1:end+2,1)=[ node(1,2); node(2,3)];
end
function sz = grN( this )
sz = size( this.nodelist, 1 );
end
function this = AddNode( this, a ,b )
this.nodelist(end+1) = node( a, b );
end
end
end
  댓글 수: 2
Daniel Shub
Daniel Shub 2012년 3월 25일
My guess is that at some point Wu is going to subclass node and therefore need to use a heterogeneous array. As it stands now, I like your way with standard arrays.
per isakson
per isakson 2012년 3월 26일
@Daniel, Thanks! Yes, but this is just a little exercise.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by