MATLAB: Modify a surface within a class

classMATLABobjectooppropertiessurfsurface

Hi! I have a quick question about how object-oriented programming works in matlab, specificly involving saving a Surface to the properties of the object.
I am trying to write a class to plot 3D object using surf(X,Y,Z). Is it possible to save the surface object that it draws, to be modified later? Here is some code to demonstrate my question:
classdef cylinDraw
properties
r
h
surface
end
methods
function obj = cylinDraw(r,h)
%Save new size

obj.r = r;
obj.h = h;
%Create XYZ Cordinates for Cylinder

[X,Y,Z] = cylinder(obj.r);
Z = Z * obj.h;
surface = surf(X,Y,Z); % <--
end
function changeSize(obj, r, h)
%Save new size
obj.r = r;
obj.h = h;
%Create XYZ Cordinates for Cylinder
[X,Y,Z] = cylinder(obj.r);
Z = Z * obj.h;
%Replace surface XYZ Values (Doesn't Work)
surface.XData = X;
surface.YData = Y;
surface.ZData = Z;
end
end
end
If this code was run in matlab:
foo = cylinDraw(2,3);
foo.changeSize(5,4);
The Surface from the surf(X,Y,Z) command in the constructor would not be saved, causing the method changeSize to not actualy change the size of the cylinder in the plot. Is there a way to store/link the drawn surface to be able to do this?
If it matters, the application for the class I am writting is to (eventualy) create animation/live display of the location of objects in real space as they move around with 6DOF, with models of the actual objects.

Best Answer

You already have a class property reserved for the surface handle, but you are not using it. Simply do,
obj.surface = surf(X,Y,Z);
in the constructor, and similarly in other class methods. Also, it appears as though you intend for cylinDraw to be a handle class, so you should declare it as such,
classdef cylinDraw < handle