class: how to use the class within itself
40 views (last 30 days)
Show older comments
Hi there, I have used Processing (based on java) and recently turned to Matlab, and its syntax is quiet confusing..I am trying to make a simple spring simulation using class, and in the function 'reaction', I would like to use the object from this class as an input, and in the function 'move', I'd like to use the property 'position' initialised in the beginning and change it constantly.
But when I call this class, millions of errors occurs..can anybody help to tell me where I did wrong..I got something saying that the property 'position'must be accessed from a class instance because it is not a constant property..And I've never seen that..
classdef Particle
properties
rest = 100;
mass=1000;
damping=0.999;
position=[0 0];
velocity; %= [1.*rand(100,1) 1.*rand(100,1)];
end
methods (Static)
function this = Particle()
this.position=[0 0];
this.velocity=[1.*rand(100,1) 1.*rand(100,1)];
end
function velocity=react(Particle)
p1 = Particle.position;
p2 = Particle.position;
d = sqrt((p1(1)-p2(1))^2+(p1(2)-p2(2))^2);
displacement = d-Particle.rest;
a = normr(p1-p2);
a = a.*(displacement/Particle.mass);
velocity = Particle.velocity+a;
% Particle.bounce(position, velocity);
velocity = velocity.*Particle.damping;
end
function [position] = move (velocity)
position=Particle.this.position+velocity;
end
function draw (myposition)
viscircles(myposition, 0.1);
end
end
end
and this is my main sketch calling class
a = Particle;
b = Particle;
a.react(b);
b.react(a);
a.move();
b.move();
line([a.position(1) b.position(1)],[a.position(2) b.position(2)]);
a.draw();
b.draw();
Thanks so much! I think I'm still using the java syntax rather than matlab's....
0 Comments
Accepted Answer
David Ding
on 25 May 2017
Hi Vapeur,
It is difficult to tackle the issue without seeing the actual error. However, seeing your code, there is an obvious error in your methods, which is that you must pass in the object as the first argument of each method. This is expected for MATLAB. For example, you have:
function this = Particle()
this.position=[0 0];
this.velocity=[1.*rand(100,1) 1.*rand(100,1)];
end
The syntax is incorrect. Instead you need to have:
function Particle(obj)
obj.position=[0 0];
obj.velocity=[1.*rand(100,1) 1.*rand(100,1)];
end
Note the first argument in the "Particle" method is the object itself. Also this is a void function so no output is specified.
Another example is with regards to your "move" method.
function [position] = move (velocity)
position=Particle.this.position+velocity;
end
I am guessing that you are outputting the updated position by the velocity input. Remember that the first argument MATLAB expects is the object itself. So you need to have something like this:
function [position] = move (obj, velocity)
position = obj.position+velocity;
end
Try to fix other parts of your code in the methods section. For example, in your "react" method, you need to start with:
function velocity = react(obj)
...
end
Best of luck on your endeavors!
Thanks,
David
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Java from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!