Prev: [HELP]how to write matlab program using PI based on LQR
Next: [HELP]how to write matlab program using PI based on LQR
From: Matthew on 25 Jul 2010 02:32 Hi. I've made a class. For simplicity, we'll call it A. I want to create a class B, such that it contains an array of A objects. I also won't know how many A objects to create when I initialize B, so I'll have to dynamically allocate memory for the A objects. However, in MATLAB it seems I cannot do this... classdef A properties A1 A2 end methods function A_object = A(test) end end classdef B properties Array_of_A num_elements end methods function B_object = B(test) num_elements=0; end function num = AddAObject(B_Object,A_Object) num_elements=num_elements+1; B_Object.Array_of_A(num_elements)=A_Object; end end Thanks for your help, Matt
From: Matt J on 25 Jul 2010 10:35
It seems excessive to create a function AddAObject to do this. In default MATLAB behavior, you can just do B_Object.Array_of_A(end)=A_Object; which is about the most keystroke-efficient thing you can do if B is not a handle class. But, if you insist, the following will fix your existing code: classdef B properties Array_of_A num_elements end methods function B_object = B(test) B_object.num_elements=0; end function B_Object = AddAObject(B_Object,A_Object) B_Object.num_elements=B_object.num_elements+1; B_Object.Array_of_A(num_elements)=A_Object; end end |