Trouble with creating classes

조회 수: 7 (최근 30일)
Petya Popova
Petya Popova 2012년 6월 7일
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?

채택된 답변

per isakson
per isakson 2012년 6월 7일
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

추가 답변 (2개)

Petya Popova
Petya Popova 2012년 6월 7일
Thanks!
I first copied by mistake my very first attempt that's why it didn't have any referencing :), but thanks again! I edited it now. Should I use set. or get. Also can I use something different than obj or this?
I use set and i'm not sure the situation got any better...i got from this:
>> N19=Players(19,'Ivanov',t,x,y,z)
N19 =
Players
Properties:
Number: 19
Name: 'Ivanov'
t: [146x1 double]
x: [146x1 double]
y: [146x1 double]
z: [146x1 double]
Data: []
Acceleration: []
Speed: []
Position: []
Methods
to this:
>> N19=Players(19,'Ivanov',t,x,y,z)
N19 =
Players
Properties:
Number: 19
Name: 'Ivanov'
t: [146x1 double]
x: [146x1 double]
y: [146x1 double]
z: [146x1 double]
Methods
  댓글 수: 3
per isakson
per isakson 2012년 6월 7일
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.
per isakson
per isakson 2012년 6월 7일
The name "obj" is useful for other objects, which are passed to a method.

댓글을 달려면 로그인하십시오.


Petya Popova
Petya Popova 2012년 6월 7일
Solved it! Thanks again!

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by