[Scilab-users] Convert struct to mlist/tlist
Samuel Gougeon
sgougeon at free.fr
Thu Jul 20 12:23:32 CEST 2017
Hello,
Le 20/07/2017 à 10:01, Pierre Payen a écrit :
> .../...
>
> // BONUS : with getfield, you can get the value inside nested structures
> when you dont know the field name
> time_st = struct('date',date_st)
>
> fields = fieldnames(time_st)
> for j = 1:size(fields,'*')
> subfields = fieldnames(time_st(fields(j)))
> for i = 1:size(subfields,'*')
> value=getfield(subfields(i),getfield(fields(j),time_st))
> end
> end
This is possible as well with fieldnames(). With the following, we print
a 2-level nested structure. To generalize to any nesting depth,
the "else" should become recursive.
date_st = struct('day',25,'month' ,'DEC','year',2006);
event = struct('date',date_st, "place","Paris");
fevent = fieldnames(event);
for f = fevent'
v = event(f);
sfields = fieldnames(v);
if sfields==[]
printf("%s: %s\n", f, sci2exp(v));
else
for s = sfields'
printf("%s.%s: %s\n", f, s, sci2exp(v(s)));
end
end
end
-->
date.day: 25
date.month: "DEC"
date.year: 2006
place: "Paris"
-->
More information about the users
mailing list