Help with class methods please
3 次查看(过去 30 天)
显示 更早的评论
I'm pretty new to Matlab. I'm writing code that creates simulated images of rod-like particles diffusing in a liquid. I'm trying to use OOP to do this so I can keep track of positions and angles of the particles. My constructor method for the rods works fine, but I have a method to make them move and rotate that doesn't work. When I just write out the code in a function to test it it works so I'm guessing it's just something in the syntax that makes it not work when I call the method.
classdef rod
properties
rodlength;
i_pos=0;
j_pos=0;
angle;
frame;
methods
function step(d,z,stepsize)%z is the simulated image the rod is put into
I want step to change the position and angle of some rod object d (I didn't include all the code inside the method because its long and boring). When I call the method, by saying rod_1.step(frame, 50) nothing happens, i.e. the image before and after I invoke the method is the same. However if I write out all the code outside the object it works so I think it has something to do with the way I call it. Any suggestions? Thanks.
0 个评论
采纳的回答
per isakson
2012-6-21
Change
classdef rod
to
classdef rod < handle
.
--- Cont. ---
Value objects are immutable. Their state (property values) cannot be be changed. When a new value is assigned to a property of a value object a new object is created and that new object must be assigned to a variable name or it is lost.
new_object = old_object.method1( some_arguments );
It's hubris to try to write a simple explanation. I'm convinced the documentation pros at The Mathworks have tried hard to do that. "MATLAB numeric variables are of value objects."
x = 17; x + 3;
The value of x is still 17.
.
Handle objects are mutable. Their state may be changed. An object may have multiple handles. Most of the common Design Patterns apply to handle objects. The Matlab documentations hardly mentions Design Patterns.
2 个评论
Sean de Wolski
2012-6-21
Value: pass by value; handle: pass a handle around that points to the value and inherit all of the cool features of the handle class.
更多回答(1 个)
Eric
2012-6-21
I think per isakson's answer is a bit simplistic. You should probably read up on the difference between Handle classes and Value classes and then decide which type of class you need. Handle classes are not always the right solution. See
If you're only ever going to create one instance of a rod object, then a Handle class is probably fine. Without the "< handle" suffix in the classdef line, you have a Value class. In that case you should use
obj = obj.step(d, z, stepsize);
Good luck,
Eric
2 个评论
Eric
2012-6-22
My apologies, simplistic is indeed overly harsh. I just wanted to make sure the OP is aware of what he's doing and that it may induce some other unexpected behaviors. I've had colleagues suffer from this same issue with Matlab classes. They've gone to great lengths in writing overly complicated code because they think Matlab only allows shallow copies of objects. I believe they may have been using the Handle class type without fully understanding what that means.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!