On Thu, Nov 5, 2009 at 11:00 PM, Guido Walter Pettinari
<address@hidden> wrote:
Hello everybody!
Sorry for this late reply but I had much work to do in these days.
Thank you for all the answers. It was very instructive to learn all
the methods to handle with field names; I find particularly useful
what Benjamin pointed out, that is the possibility to create/access a
field via strings by using the parentheses:
for i = 1:N
field = sprintf ( 'something_%d', i );
S.(field) = whatever(i);
endfor
In this way the structures become really flexible!
@Carlo
but still I don't understand why you want to call your vectors
S.ks_ii,
I'm really curious: what's the problem with calling them KS{ii}?
I want to call the vectors in that way because I want a struct where
the fields are just column vectors. I like to access everything in
the
structure S by just typing S.k1 or S.k2 rather than S.KS{i}. This is
just a personal preference, probably because I mainly program in C++
and I do not feel comfortable with cell arrays yet :)
Cheers,
Guido
While it probably won't matter until number get really big, you should
also realize that the syntax
S.ks{i} is not only more natural (requires no string manipulation
whatsoever), it is also more efficient.
When you give S a thousand fields ks1...ks1000, then s.ks200 requires
a binary search in 1000 strings, while
s.ks{i} doesn't. Your approach is asymptotically inferior.
In other words, the arguments for using cell arrays are the same as
for using arrays of numbers rather than name-mangled scalars.
regards
--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz