[Scilab-users] Convert struct to mlist/tlist

classic Classic list List threaded Threaded
4 messages Options
shamikam shamikam
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Scilab-users] Convert struct to mlist/tlist

Hello,

I'm using Scilab 5.5.0. How do I convert a struct to an mlit/tlist in Scilab code?

For example, I have created a struct using date_st=struct('day',25,'month' ,'DEC','year',2006)

Is there any way to get the number of fields in the struct so that I can loop over the struct? I want to do this operation for a struct with unknown number of fields and values.

Regards,
Shamika

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Convert struct to mlist/tlist

Hello Shamikan,

A (array of) structures IS a mlist. The list of defined fields is returned by fieldnames():

--> s.b = %t;
--> s.r = %pi;
--> s.p = %z
 s  =
  b: [1x1 boolean]
  r: [1x1 constant]
  p: [1x1 polynomial]


--> fieldnames(s)
 ans  =
!b  !
!   !
!r  !
!   !
!p  !

And the number of fields:
--> size(fieldnames(s),1)
 ans  =
   3.

Regards
Samuel Gougeon
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Pierre Payen Pierre Payen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Scilab-users] Convert struct to mlist/tlist

This post was updated on .
In reply to this post by shamikam
Also using the getfield function 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 thus are useless
fields = fields(3:$) // same as fieldnames(date_st)

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

// now you can use 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 don't 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

////////////////////////////////////////////////////////////////////////////////
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Convert struct to mlist/tlist

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


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Loading...