Unable to append zeros to the vectors and matrices from within the member functions

1 vue (au cours des 30 derniers jours)
Hi, I am trying to implement a class for a growing cylinderical organ,
objectibe: My constructor initiallizes the vectors and matrices whose sizes I want to increase with each iteration. This increase is size is achieved by calling growth function by appending zeros at the end of each vector and Matrix.
Issue: The problem is, I am unable to modify the size of the vectors and matrices from within the growth(obj) function. There is no errror, I even run my code step by step and check the size of my vectors at each iteration using size() command. The size remians the same as initialized, even though the growth(obj) function is successively called and run. I confirmed it by displaying messages from within the growth (obj) function at each iteration.
here is my code,
classdef Organ_Python
% Detailed explanation goes here
properties
% % % k1 =[]; %curvature
% % % k2 =[]; %curvature
% % % r =[]; %centerline
% % % D =[]; %rotation matrices to centerline's local coordiates
% % % NUM; %Number of segments
% % % NUM_gz; %Number of segments in the growth-zone
% % % ds; %segment length
% % % R; %organ's radius
% % % phi = [];
% % % kappa = [];
% % % B = [];
% % % N = [];
k1(1,:); %curvature
k2(1,:); %curvature
r (3,:); %centerline
D (3,:,3); %rotation matrices to centerline's local coordiates
NUM; %Number of segments
NUM_gz; %Number of segments in the growth-zone
ds; %segment length
R; %organ's radius
phi(1,:);
kappa;
B (3,:);
N (3,:);
end
methods
% constructor function to initialize vectors and matrices
function organ = Organ_Python(NUM,NUM_gz,ds,R) %Starts as a straight organ pointing to the z direction
organ.k1 = zeros(1, NUM); %curvature
organ.k2 = zeros(1, NUM); %curvature
organ.phi = zeros(1, NUM); %twist
organ.kappa = zeros(1); %curvature
organ.r = zeros(3,NUM); %centerline
organ.B = zeros(3,NUM);
organ.N = zeros(3,NUM);
organ.D = zeros(3,NUM,3); %rotation matrices to centerline's local coordiates
organ.NUM = NUM; %Number of segments
organ.NUM_gz = NUM_gz; %Number of segments in the growth-zone
organ.ds = ds; %segment length
organ.R = R; %organ's radius
%centerline initializtion:
organ.D(:,1,:)=eye(3);
organ.Update_centerline()
end
function growth(obj) %increasing the length of the organ by one segment
obj.k1= [obj.k1 0*ones(1)];
obj.k2= [obj.k2 0*ones(1)];
obj.phi=[obj.phi 0*ones(1)];
obj.r = [obj.r zeros(3,1)];
obj.N = [obj.N zeros(3,1)];
obj.B = [obj.B zeros(3,1)];
obj.D = [obj.D zeros(3,1,3)];
%initializtion of the new segment:
%U=organ.Darboux_Matrix(1); %Darboux skew-symmetric matrix
U = reshape(obj.D(:,end-1,:),[3,3])*reshape([[0,0,obj.k1(end-1)],[0,0,obj.k2(end-1)],[-obj.k1(end-1),-obj.k2(end-1),0]],[3,3])*(reshape(obj.D(:,end-1,:), [3,3]))';
obj.D(:,end,:)=expm(obj.ds*U)*reshape(obj.D(:,end-1,:), [3,3]); %Rotation of the local coordinates using the Rodrigues formula
obj.r(:,end)=obj.r(:,end-1)+obj.ds*reshape(obj.D(2,end-1,:), [3,1]); %centerline update
obj.NUM=obj.NUM+1; %increase number of segements
obj.k1(end)=obj.k1(end-1); %intializaing curvature
obj.k2(end)=obj.k2(end-1); %intializaing curvature
end
end
end

Réponse acceptée

Matt J
Matt J le 28 Avr 2021
Modifié(e) : Matt J le 28 Avr 2021
Either make your class a handle class
classdef Organ_Python<handle
or return a modified object from the growth() method
function obj=growth(obj)
Note that only with the first approach will you be able to have growth() cause in-place changes in your objects. With the latter method, you will only see the changes in the object returned as output from a call to growth().
  1 commentaire
Abdul Jabbar
Abdul Jabbar le 28 Avr 2021
@Matt J, Thank you very much for your help. I the tried first method ("classdef Organ_Python <handle") and it worked for me exactly as I wanted. I really appreciate your support.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by