Reputation: 1
I have created this two codes classes.
classdef master < matlab.mixin.Copyable
properties
id
list
end
methods
function this=master(id)
if nargin > 0
this.id = id;
this.list = repmat(msg,1,20);
end
end
end
end
classdef msg < matlab.mixin.Copyable
properties
id
dest
ttl
end
methods
function this=msg(id,dest,ttl)
if nargin > 0
this.id = id;
this.dest = dest;
this.ttl = ttl;
end
end
end
end
In another part of my code I try to delete one or more objetc "msg" from array "master.list" using the following:
function verifyMsgToDiscard(this,t)
i = 1;
while (i <= numel(this.list))
m = this.list(i);
if (t > m.ttl)
this.list = this.list(this.list~=m); %remove m of the list
clear m; %delete m from the system
end
i= i + 1;
end
end
I am getting the error:
Index exceeds matrix dimensions.
Error in master/verifyMsgToDiscard (line 117) m = this.list(i);
The problem I think is why I am iterating over the master.list in the same time I modify the number of elements on it. In addition I can add and remove new objects "msg" in the "list" then its size varies. How I can do this in a dynamic way.
Upvotes: 0
Views: 216
Reputation: 551
I suppose you're trying to remove msg objects having ttl
to be smaller than t
. This is the Matlab way of removing elements:
this.list = this.list(t <= [this.list.ttl]);
Note that t <= [this.list.ttl]
generates a logical index.
Upvotes: 1