类的定义
classdef MyClass < SuperClass1 & SuperClass2
properties
Prop1
Prop2
end
methods
function obj = MyClass(input1, input2)
obj.Prop1 = input1;
obj.Prop2 = input2;
end
function output = myMethod(obj, input)
output = obj.Prop1 + obj.Prop2 + input;
end
end
end
值类型和引用类型

属性控制

SetAccess = immutable:只能赋值一次,然后锁定Constant = true构造函数
classdef MyClass
properties
Prop1
Prop2
end
methods
function obj = MyClass(input1, input2)
obj.Prop1 = input1;
obj.Prop2 = input2;
end
end
end
function myTimer()
t = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @myCallback);
start(t);
end
function myCallback(src, event)
disp('Timer fired!');
end
其他参数
'TasksToExecute', 5 :表示定时器将在执行5次操作后自动停止。'BusyMode', 'queue’:表示如果定时器在执行操作时遇到堵塞,它将等待操作完成后再执行下一个操作。'StopFcn', @myStopCallback:定时器结束回调#如果计时器是一个类成员
classdef lightController < handle
properties
state=0;
lightUI
t
greenTime=5;
yellowTime=1;
redTime=2;
greenCount=0;
yellowCount=0;
redCount=0;
end
methods
function obj=lightController()
obj.lightUI=lightUI;
obj.lightUI.controller=obj;
obj.t=timer;
[email protected];
obj.t.Period=1;
obj.t.ExecutionMode='fixedRate';
end
function countOneSec(Obj,~,~)
switch Obj.state
case 0
case 1
if Obj.greenCount>=Obj.greenTime
Obj.state=2;
Obj.greenCount=0;
else
Obj.greenCount=Obj.greenCount+1;
end
case 2
if Obj.yellowCount>=Obj.yellowTime
Obj.state=3;
Obj.yellowCount=0;
else
Obj.yellowCount=Obj.yellowCount+1;
end
case 3
if Obj.redCount>=Obj.redTime
Obj.state=1;
Obj.redCount=0;
else
Obj.redCount=Obj.redCount+1;
end
end
Obj.lightUI.changeColor(Obj.state);
end
end
end