Trouble with creating classes
8 views (last 30 days)
Show older comments
Hi!
First I have to shamefully admit that I have a very little experience with programming and I'm very new to classes in Matlab, so please bear with me :) My question is the following:
I want to have a class which has lets say a football player's name, number, acceleration, speed and position. The initial data I want to enter when creating the class with a constructor is the name,number and t,x,y,z, which are the time and raw acceleration in three axes. So far, so good. I know I can call the functions I have written in the class methods,but it seems that I'm not doing it right. For example the properties I want to have besides the initial ones are Data(which really is just a matrix of [t x y z], but I can't even manage to do that right!), Acceleration,Speed,Position. This is as far as I've come please don't laugh too much :)
Thanks in advance!
classdef Players
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
Number=0
Name='';
t=0
x=0
y=0
z=0
end % end properties
properties ( SetAccess = private)
Data
Acceleration
Speed
Position
end % end properties
methods
function pl = Players(Number,Name,t,x,y,z)
if nargin > 0 % Support calling with 0 arguments
pl.Number = Number;
pl.Name = Name;
pl.t = t;
pl.x = x;
pl.y = y;
pl.z = z;
end
end % Players
function obj = set.Data(obj, value)
value=[pl.t pl.x pl.y pl.z];
obj.Data= value;
end
function obj = set.Acceleration(obj, rot)
[ Mb] = butterworth( pl.Data );
[d,p,h]=calibration(Mb);
rot=h*9.81;
obj.Acceleration = rot;
end
function obj = set.Speed(obj, Mv)
[Mv] = velocity(pl.t,pl.Acceleration);
obj.Speed= Mv;
end
function obj = set.Position(obj, Mp)
[Mp] = position(pl.t,pl.Speed);
obj.Position= Mp;
end
end % end methods
end
P.S. Also if i use classes in a GUI do they need to be handle classes?
0 Comments
Accepted Answer
per isakson
on 7 Jun 2012
With GUI you can use both handle and value classes. Handle classes and handles in Handle Graphic are not "connected". Many of the standard design patterns only works with handle classes.
One problem with you code is that in the methods you refer to properties with "name". It should be "obj.name". With Matlab one must refer to the object explicitely.
The Code Analyzer is happy with this code and the box is green. I prefer the word "this" to "obj".
classdef Players
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
Number=0
Name='';
t=0
x=0
y=0
z=0
end % end properties
properties ( SetAccess = private)
Data
Acceleration
Speed
Position
end % end properties
methods
function this = Players( Number, Name, t, x, y, z)
if nargin > 0 % Support calling with 0 arguments
this.Number = Number;
this.Name = Name;
this.t = t;
this.x = x;
this.y = y;
this.z = z;
end
end % Players
function this = get.Data(this)
if nargin > 0
this.Data=[ this.t, this.x, this.y, this.z ];
end
end
% function rot = get.Acceleration( this )
% Mb = butterworth( this.Data );
% [ d, p, h ] = calibration( Mb );
% rot= h*9.81;
% end
function Mv=get.Speed( this )
Mv = velocity( this.t, this.Acceleration);
end
function Mp=get.Position( this )
Mp = position( this.t, this.Speed );
end
end% end methods
end
0 Comments
More Answers (2)
Petya Popova
on 7 Jun 2012
3 Comments
per isakson
on 7 Jun 2012
I guess set and get-methods should be used only when there is a good reason. get-methods can be a nuisance during development because they are called by tooltip and if there is an error in the method it will be triggered when one use tooltip.
See Also
Categories
Find more on Data Type Identification 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!