Preserve the methods of an object after it is updated

2 views (last 30 days)
classdef AgentAlgorithm < handle
properties (GetAccess = 'public', SetAccess = 'private')
Detections = []
State = []
Tracks = []
end
properties (GetAccess = 'public', SetAccess = 'private') %PROTECTED
Batch; %Used to store previous states and properties
%Batch(1) = T-1 state, Batch(2) = T-2 state, so on so forth
end
properties (Hidden, Constant)
MaxBatchLength = 5;
end
methods
%Constructor of Agent
function Agent = AgentAlgorithm(State)
Agent.State = State;
NewBatch = Agent.CreateBatch([], [], []);
Agent.Batch = NewBatch;
end
%New State & Detections initilizer for Agent
function Agent = AgentUpdate(Agent, NewState, NewDetections)
%Create last state Batch
NewBatch = Agent.CreateBatch(Agent.Detections, Agent.Tracks, Agent.State);
%If Batch is empty (Not initilized yet)
if isempty(Agent.Batch(1).State)
Obj.Batch = NewBatch;
else
Obj.Batch = [NewBatch Agent.Batch];
end
%Update with new parameters
Obj.State = NewState;
Obj.Detections = NewDetections;
Agent = Obj;
end
end
methods (Static, Access = 'private')
%Creates a new batch with given parameters
function NewBatch = CreateBatch(Detections, Tracks, State)
NewBatch = struct('Detections', Detections,...
'Tracks', Tracks, 'State', State);
end
end
end
Question: When I call this object as follows:
AAA = AgentAlgorithm(1)
methods(AAA)
Methods for class AgentAlgorithm:
AgentAlgorithm delete findprop isvalid ne
AgentUpdate eq ge le notify
addlistener findobj gt lt
AAA = AAA.AgentUpdate(2,3)
methods(AAA)
Methods for class struct:
amd ctranspose fieldnames ichol linsolve permute struct2cell
cholinc display fields ilu luinc reshape transpose
My problem is how can I use AgentUpdate(x,y) function such that at the end of I use this function, I still have the updated version of AAA object with all methods (AgentAlgorithm, AgentUpdate) after constructor called instead of a new AAA object w/o any user defined methods left. I used handle, did not work as I expected as you can see.

Accepted Answer

Honglei Chen
Honglei Chen on 16 Feb 2012
In your method AgentUpdate, don't use Obj. Just use AAA directly. Again, the way you wrote it now, Obj is a struct and when you do AAA=Obj at the end, your AAA becomes a struct.
function AgentUpdate(Agent, NewState, NewDetections)
%Create last state Batch
NewBatch = Agent.CreateBatch(Agent.Detections,...
Agent.Tracks, Agent.State);
%If Batch is empty (Not initilized yet)
if isempty(Agent.Batch(1).State)
Agent.Batch = NewBatch;
else
Agent.Batch = [NewBatch Agent.Batch];
end
%Update with new parameters
Agent.State = NewState;
Agent.Detections = NewDetections;
end
In addition, because AgentAlgorithm is a handle class, in your definition of the method, you don't have to put Agent as the output.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!