use object for interative loop

3 visualizzazioni (ultimi 30 giorni)
Tom Wright
Tom Wright il 16 Apr 2014
Commentato: Sean de Wolski il 16 Apr 2014
Hi, I have an object that I want to iterate through it's members with a while loop but I can't see how to call the while loop? Thanks, tom
classdef cls_myobj
properties
members = {'abc' 'bbc' 'cbc'}
length = 3
index = 0
end
methods
function [this,obj] = getNext(this)
this.index = this.index +1;
if this.index > this.length
obj = -1; %or false???
else
obj = this.members(this.index);
end
end
end
myobj=cls_myobj;
while [this, x] = getNext(myobj)
%do stuff
end

Risposta accettata

Tom Wright
Tom Wright il 16 Apr 2014
I guess I can answer this myself now
while myobj.index < myobj.length
[myobj, newthing] = myobj.getNext(myobj)
%do thing with newthing
end
  1 Commento
Sean de Wolski
Sean de Wolski il 16 Apr 2014
It doesn't look right that you have myobj.method and method(myobj). I'd expected you'd only need one or the othr
[myobj,newthing] = getNext(myobj);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Software Development Tools in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by