[Scilab-users] Convert struct to mlist/tlist

Pierre Payen payen.pierre at gmail.com
Thu Jul 20 10:01:56 CEST 2017


Also using the getfield functions gives you the field name and the field
value :

////////////////////////////////////////////////////////////////////////////////////////////////////
date_st =struct('day',25,'month' ,'DEC','year',2006)

// getting the field
fields = fieldnames(date_st) // this is a macro, output is column of strings

fields = getfield(1,date_st) // this is a primitive , output is a line of
strings
// but first and second field contains the type and the dims of the struct
so useless
fields = fields(3:$) // identical to fieldnames(date_st)

// getting the value
// all 3 give the same output
value1=date_st(fields(1))
value1=getfield(fields(1),date_st)
value1=getfield(3,date_st)

// now you can nest this in a loop
for i=1:size(fields,'*')
  value=date_st(fields(i))
  value=getfield(fields(i),date_st)
  value=getfield(2+i,date_st)
end

// 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

////////////////////////////////////////////////////////////////////////////////



--
View this message in context: http://mailinglists.scilab.org/Scilab-users-Convert-struct-to-mlist-tlist-tp4036747p4036764.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.



More information about the users mailing list