[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Lists of objects?
From: |
Joanna Rutkowska |
Subject: |
Lists of objects? |
Date: |
Thu, 25 Oct 2012 21:46:41 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121016 Thunderbird/16.0.1 |
Hello,
What is the best method to create a list of objects (all of the same
class)? I tried this:
list = [obj1, obj2, obj3];
... but when displaying such a list (the class defines own display()
method) I can see how the calls to each display () method got screwed
up, and intermixed, just if there was some race condition problem
somewhere, e.g.:
octave:2> motors_list
Robbe ROXXY 2827/34 motor:
--------------------------
MK 3638 motor:
--------------------------
Kv = 760.000000 [RPM/V]
Kv = 770.000000 [RPM/V]
Rm = 0.244000 [Ohm]
Rm = 0.700000 [Ohm]
I0 = 0.500000 [A]
I0 = 0.060000 [A]
mass = 60 [g]
mass = 132 [g]
max current (continues) = 9.0 [A]
max current (continues) = 20.0 [A]
source: ecalc.ch
source: Google :)
When I use cell array instead, i.e.:
list = {obj1, obj2, obj3}
... then the display works fine:
octave:2> motors_list
motors_list =
{
Robbe ROXXY 2827/34 motor:
--------------------------
Kv = 760.000000 [RPM/V]
Rm = 0.244000 [Ohm]
I0 = 0.500000 [A]
mass = 60 [g]
max current (continues) = 9.0 [A]
source: ecalc.ch
MK 3638 motor:
--------------------------
Kv = 770.000000 [RPM/V]
Rm = 0.700000 [Ohm]
I0 = 0.060000 [A]
mass = 132 [g]
max current (continues) = 20.0 [A]
source: Google :)
}
Anybody can explain to me why the use of standard array messes up the
output?
Also, what is the best method to build a list successively, e.g.:
obj1 = xxx ()
list = {list, obj1}
obj2 = xxx ()
list = {list, obj2}
The code above unfortunately doesn't work well, because it creates
nested cell arrays.
?
Also, what is the best method to create code that would something like that:
list1 = {c1_obj1, c1_obj2, c1_obj3, ...} // all objs of class c1
list2 = {c2_obj1, c2_obj2, ...} // objects of class c2
...
listN = {cN_obj1, cN_obj2, ...} // objects of class cN
Now, for each combination of objects from each list, do something, e.g.:
X = something (list1(i1), list2(i2), ... listN(iN)
In other words, having some N-dimensional space of possible
configurations, I would like to calculate a target function on each
possible point in this space.
The most primitive way of coding this would be, of course using the N
nested for loops, e.g.:
for i1 = 1:lengtt(list1)
for i2 = 1:length(list2)
...
for iN = 1:length(listN)
X = something (list1(i1), list(i2), ..., list(iN))
but I was curious if there is a mechanism in matlab to do that somehow
nicer?
Thanks,
joanna.
- Lists of objects?,
Joanna Rutkowska <=