필터 지우기
필터 지우기

Help with class methods please

조회 수: 2 (최근 30일)
John Kerin
John Kerin 2012년 6월 21일
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.

채택된 답변

per isakson
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
John Kerin
John Kerin 2012년 6월 21일
Thanks this seems to work! What's the simple explanation for the difference between value and handle classes?
Sean de Wolski
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
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
per isakson
per isakson 2012년 6월 21일
I would rather call it a "hint".
Eric
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 CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by