0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.