Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.0.0 - English


String management

How to manage Scilab's String read and write process using call_scilab and api_scilab

Description

This help describes how strings and matrix of strings can be handle through the Call Scilab API and api Scilab.

There are several functions which can be used to read / write from the memory of Scilab. These functions are described in dedicated pages.

Note: Access to variables is done through api Scilab (named variable).

Examples

// This example shows how to write a Scilab string in Scilab engine
// It is the equivalent to A="my Message"; in Scilab interpreter
// See: modules/call_scilab/examples/basicExamples/readwritestring.c

// StartScilab
int row = 1, col = 1; /* Size of the matrix */
/* Declare the string */
char **myMatrixOfString = (char**)malloc(sizeof(char*) * row * col);
myMatrixOfString[0]="my Message";
char variableName[] = "A";
SciErr sciErr;

/* Write it into Scilab's memory */
sciErr = createNamedMatrixOfString(pvApiCtx, variableName, row, col, myMatrixOfString);
if(sciErr.iErr)
{
    printError(&sciErr, 0);
}

/*
 * Prior to Scilab 5.2
 * C2F(cwritechain)(variableName, &sizeOfMyString  , myString, strlen(variableName), sizeOfMyString);
*/

printf("Display from Scilab of A:\n");
SendScilabJob("disp(A);"); /* Display A */
/* Load the previously set variable A */
// See: modules/call_scilab/examples/basicExamples/readwritestring.c

char variableToBeRetrieved[]="A";
int iRows       = 0;
int iCols       = 0;
int i,j;
int* piAddr     = NULL;
int* piLen      = NULL;
char** pstData  = NULL;
SciErr sciErr;

//first call to retrieve dimensions
sciErr = readNamedMatrixOfString(pvApiCtx,variableToBeRetrieved,&iRows, &iCols, NULL, NULL);
if(sciErr.iErr)
{
    printError(&sciErr, 0);
}

piLen = (int*)malloc(sizeof(int) * iRows * iCols);
//second call to retrieve length of each string
sciErr = readNamedMatrixOfString(pvApiCtx,variableToBeRetrieved, &iRows, &iCols, piLen, NULL);
if(sciErr.iErr)
{
    printError(&sciErr, 0);
}

pstData = (char**)malloc(sizeof(char*) * iRows * iCols);
for(i = 0 ; i < iRows * iCols ; i++)
    {
        pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
    }
//third call to retrieve data
sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, piLen, pstData);
if(sciErr.iErr)
{
    printError(&sciErr, 0);
}

printf("\n");
printf("Load and display of A:\n");
for(j = 0 ; j < iCols ; j++)
{
    for(i = 0 ; i < iRows ; i++)
    {
        /* Display the formatted matrix with same scilab index */
        printf("[%d,%d] = %s\n",j+1,i+1,pstData[j* iRows + i]);
    }
}

printf("\n");
free(piLen);
for(i = 0 ; i < iRows * iCols ; i++)
    {
        free(pstData[i]);
    }
free(pstData);

See also

  • Call_Scilab — call_scilab is an interface which provides the ability to call Scilab engine from C/C++ code
  • api Scilab — api_scilab is the Scilab 6 interface to access Scilab variables.
  • SendScilabJob — Send a Scilab task from a C/C++ code (call_scilab)
  • StartScilab — Initializes and starts Scilab engine in Call Scilab
  • Call_Scilab: Boolean Management — How to manage Scilab's boolean read and write process using call_scilab and api_scilab
  • Call_Scilab: Double Management — How to manage Scilab's variable read and write process using call_scilab and api_scilab
  • Complex Management — How to manage Scilab's complex variable read and write process using call_scilab
Report an issue
<< StartScilab call_scilab API (Scilab engine) TerminateScilab >>

Copyright (c) 2022-2023 (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:
Tue Oct 24 14:30:10 CEST 2023