Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - Русский


API struct functions

This page lists all the functions to manipulate functions to manipulate Struct objects.

scilabVar scilab_createStructMatrix( scilabEnv env, int dim, const int* dims)

Create a struct matrix with dim dimensions (dims[0], dims[1], ..., dims[dim-1]).

scilabVar scilab_createStructMatrix2d( scilabEnv env, int row, int col)

Create an struct matrix with 2 dimensions (row, col).

scilabVar scilab_createStruct( scilabEnv env)

Create a scalar struct.

scilabStatus scilab_addFields( scilabEnv env, scilabVar var, int count, const wchar_t** fields)

Add count fields fields in struct var.

scilabStatus scilab_addField( scilabEnv env, scilabVar var, const wchar_t* field)

Add field field in struct var.

int scilab_getFields( scilabEnv env, scilabVar var, wchar_t*** fields)

Get field names fields from struct var and returns count of fields.

scilabVar scilab_getStructMatrixData( scilabEnv env, const wchar_t* field, const int* dims)

Get field value at position (index[0], index[1], ..., index[dims-1]) from var.

scilabVar scilab_getStructMatrixData2d( scilabEnv env, const wchar_t* field, int row, int col)

Get field value at position (row, col) from var.

scilabStatus scilab_setStructMatrixData( scilabEnv env, scilabVar var, const wchar_t* field, const int* index, scilabVar data)

Set field value at position (index[0], index[1], ..., index[dims-1]) in var.

scilabStatus scilab_setStructMatrixData2d( scilabEnv env, scilabVar var, const wchar_t* field, int row, int col, scilabVar data)

Set field value at position (row, col) in var.

Examples

#include <ctype.h>
#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include "sciprint.h"
#include "sci_malloc.h"
#include "os_string.h"

const char fname[] = "struct_test";

int sci_struct_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
{
    int i = 0;
    //input
    scilabVar in1 = NULL;
    int size1 = 0;
    wchar_t** fields = NULL;
    scilabVar in2 = NULL;
    int size2 = 0;
    //output
    scilabVar out1 = NULL;

    //goal is to take string vector and list from intput to
    //create a struct with fields names from string and
    //fields data from list.

    if (nin != 2)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    if (nout != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

    //in1
    in1 = in[0];
    if (scilab_isString(env, in1) == 0 || scilab_isVector(env, in1) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string vector expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

    size1 = scilab_getSize(env, in1);
    scilab_getStringArray(env, in1, &fields);

    //in2
    in2 = in[1];
    if (scilab_isList(env, in2) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A list expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    size2 = scilab_getSize(env, in2);

    if (size1 != size2)
    {
        Scierror(999, _("%s: Arguments #%d and #%d: Same sizes expected.\n"), fname, 1, 2);
        return STATUS_ERROR;
    }

    out1 = scilab_createStruct(env);

    for (i = 0; i < size1; ++i)
    {
        scilab_addField(env, out1, fields[i]);
        scilab_setStruct2dData(env, out1, fields[i], 0, 0, scilab_getListItem(env, in2, i));
    }

    out[0] = out1;
    return STATUS_OK;
}

Scilab test script

mkdir(pathconvert(TMPDIR+"/api_c/"));
cd(pathconvert(TMPDIR+"/api_c/"));
copyfile(SCI+"/modules/api_scilab/tests/unit_tests/api_c/struct_test.c",pathconvert(TMPDIR+"/api_c/struct_test.c",%F));

ilib_build("libstruct",["struct_test","sci_struct_test", "csci6"],"struct_test.c",[],"","","");
exec("loader.sce");

fields = ["double","string","boolean","poly","list","cell"];
data = list(1, "2", %t, %s, list(1,2,3), {1,2;3,4});
t = struct_test(fields, data);

assert_checkequal(typeof(t), "st");
assert_checkequal(t.double, 1);
assert_checkequal(t.string, "2");
assert_checkequal(t.boolean, %t);
assert_checkequal(t.poly, %s);
assert_checkequal(t.list, list(1,2,3));
assert_checkequal(t.cell, {1,2;3,4});
Report an issue
<< API cell functions API Scilab API handle functions >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:18:38 CEST 2024