Looking through a Matlab Class

Hi!
I would like to execute some actions on every object of my class, say "Island".
Is there a command in Matlab allowing this? like
for island = Island
% actions to do
end
I hope I've made myself understood :)
Thank you!
Martin

 Respuesta aceptada

Matt J
Matt J el 27 de Mayo de 2013

0 votos

if you have an array, obj, of Island objects, you would just do things like
for i=1:length(obj)
obj(i).prop=...
end

Más respuestas (1)

Martin
Martin el 27 de Mayo de 2013
Editada: Matt J el 28 de Mayo de 2013

0 votos

Ok, thank you. It seems that there is no other solution :).
Another question, which may appear a bit silly :
If I create an array :
Skye = 1
Corse = 2
All_Islands = [Skye Corse]
Is there a way, only using All_Islands, to find the name of the variables (Skye & Corse) and not only there values (1 % 2)?
Sounds like a beginner's question and seems like the answer is no, but we never know... :)
Thanks!

4 comentarios

Matt J
Matt J el 28 de Mayo de 2013
Ok, thank you. It seems that there is no other solution :).
Why would there need to be?
Is there a way, only using All_Islands, to find the name of the variables (Skye & Corse) and not only there values (1 % 2)?
No, but if you describe your motive, it may point to better approaches.
Martin
Martin el 28 de Mayo de 2013
My first remark was pointing the fact that I was obliged to create an array to look over all the elements of a class. I was simply wondering whether a command like "for island in Islands" existed.
For the second, I was wondering if, by just looking over the array AllIslands, it would have been possible to get the names of the variables (to display them with their associated values for example).
Daniel Shub
Daniel Shub el 28 de Mayo de 2013
@Martin, what you are asking to do seems a little bit strange (both the getting all objects and the getting the variable names). I think they are two separate questions and should be asked as such (you are likely to get better answers). In order to get the best answers, you will likely need to provide some broader insight into what you are trying to accomplish since my guess is you are too focused on the problem right now. Finally, please use the question, comment, and answer fields appropriately as it makes it easier to follow what is going on.
Martin
Martin el 28 de Mayo de 2013
Ah yeah I just got the difference between comment and answer :) .
Never mind for the second question, as I said it's probably stupid. And the first one has been answered :)
Thank you Matt and Daniel!

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Mayo de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by