[Scilab-Dev] How to create a Scilab structure ?
Yann Collette
yann.collette at scilab.org
Wed Aug 19 16:05:31 CEST 2009
Vincent a écrit :
> Hi,
>
> I would like to create a SciLab structure from C code. By scilab structure I
> mean a variable that has some "children", for instance, as outputed by scilab
> :
>
> a =
>
> a1: 1
> a2: 2
> a3: 3
> a4: 4
>
> also with structure member instancied by a.a1=1, a.a2=2...
> It appears that such object are neither list nor tlist. So I have no idea how
> to produce structure like that...
>
> Regards,
> Vincent.
>
>
>
A structure is like a mlist:
a = struct();
a.a1 = 1;
a.a2 = 2;
a.a3 = [1 2 3 4];
getfield(1,a) = !st dims a1 a2 a3 !
'st' is the type of the "struct" mlist and corresponds to the values
returned by 'typeof(a)'
'dims' stores the dimension of a and is the value returned by 'size(a)'
and the remaining things are the labels.
You can access the values like a matlab struct:
disp(a.a1)
or like a Scilab mlist
disp(a('a1'))
To create such a structure using the new api_scilab:
int sci_new_api_mlist_out(char * fname)
{
int res, i;
int * p_out_address;
char *pstLabels[] = {"st","dims","a1","a2","a3"};
double pdblDims[] = {1,1};
double pdblA1[] = {1};
double pdblA2[] = {1};
double pdblA3[] = {1,2,3,4};
res = createMList(1, 5, &p_out_address);
// Add the labels at position 1 (in the first field).
// We can get the labels using getfield(1,mymlist) in Scilab
createMatrixOfStringInList(1, p_out_address, 1, 1, 5, pstLabels);
// Add a vector to the 'dims' field
createMatrixOfDoubleInList(1, p_out_address, 2, 1, 2, pdblDims);
// Add the 'Ai' fields
createMatrixOfDoubleInList(1, p_out_address, 3, 1, 1, pdblA1);
createMatrixOfDoubleInList(1, p_out_address, 3, 1, 1, pdblA2);
createMatrixOfDoubleInList(1, p_out_address, 3, 1, 4, pdblA3);
// Return the created mlist
LhsVar(1) = 1;
return 0;
}
YC
More information about the dev
mailing list