Trouble with creating classes
古いコメントを表示
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?
採用された回答
その他の回答 (2 件)
Petya Popova
2012 年 6 月 7 日
3 件のコメント
per isakson
2012 年 6 月 7 日
You can use any legal Matlab name for the object. However, "me", "self", "this" are used in other languages and will be recognized by other programmers. Chose one and stick to it.
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
2012 年 6 月 7 日
The name "obj" is useful for other objects, which are passed to a method.
カテゴリ
ヘルプ センター および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!