/*------------------------------------------------------------*/ static int first_example(void) { void* pvApiCtx = NULL; static double A[] = {1, 2, 3, 4}; int mA = 2, nA = 2; static double b[] = {4, 5}; int mb = 2, nb = 1; printf("\nExample 1:\n"); printf("Some simple computations\n"); /* Create Scilab matrices A and b */ createNamedMatrixOfDouble(pvApiCtx, "A", mA, mA, A); createNamedMatrixOfDouble(pvApiCtx, "b", mb, nb, b); SendScilabJob("disp('A=');"); SendScilabJob("disp(A);"); SendScilabJob("disp('b=');"); SendScilabJob("disp(b);"); SendScilabJob("disp('x=A\\b');"); if ( SendScilabJob("A,b,x=A\\b;") != 0) { fprintf(stdout, "Error occurred during scilab execution (SendScilabJob)\n"); } else { double *cxtmp = NULL; int m, n, lp, i; /* Get m and n */ getNamedVarDimension(pvApiCtx, "x", &m, &n); cxtmp = (double*)malloc((m * n) * sizeof(double)); readNamedMatrixOfDouble(pvApiCtx, "x", &m, &n, cxtmp); for (i = 0; i < m * n; i++) { fprintf(stdout, "x[%d] = %5.2f\n", i, cxtmp[i]); } if (cxtmp) { free(cxtmp); cxtmp = NULL; } } return 0; }
double *getDouble(char *variableName, int *nbRow, int *nbCol) { SciErr sciErr; double *matrixOfDouble = NULL; sciErr = readNamedMatrixOfDouble(NULL, variableName, nbRow, nbCol, NULL); if (sciErr.iErr) { printError(&sciErr, 0); } /* Alloc the memory */ matrixOfDouble = (double *)malloc(((*nbRow) * (*nbCol)) * sizeof(double)); /* Load the matrix */ sciErr = readNamedMatrixOfDouble(NULL, variableName, nbRow, nbCol, matrixOfDouble); if (sciErr.iErr) { printError(&sciErr, 0); } return matrixOfDouble; }
/*------------------------------------------------------------*/ int main(void) { #ifdef _MSC_VER if ( StartScilab(NULL, NULL, 0) == FALSE ) #else if ( StartScilab(getenv("SCI"), NULL, 0) == FALSE ) #endif { fprintf(stderr,"Error while calling StartScilab\n"); return -1; } /******************************** WRITE ****************************/ /* * Write a line matrix into Scilab * A=[ 1 3 3 2 ]; */ { double A[] = {1,3,3,2}; /* Declare the matrix */ int rowA = 1, colA = 4; /* Size of the matrix */ char variableName[] = "A"; SciErr sciErr; /* * Write it into Scilab's memory */ sciErr = createNamedMatrixOfDouble(pvApiCtx,variableName,rowA,colA, A); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(cwritemat)(variableName, &rowA, &colA, A,strlen(variableName)); */ printf("Display from Scilab of A:\n"); SendScilabJob("disp(A);"); /* Display A */ } /* * Write a matrix into Scilab * B=[1 4 2 3; * 3 9 8 2 ] * Note that it is done column by column */ { double B[] = {1,3,4,9,2,8,3,2}; /* Declare the matrix */ int rowB = 2, colB = 4; /* Size of the matrix */ char variableNameB[] = "B"; SciErr sciErr; /* * Write it into Scilab's memory */ sciErr = createNamedMatrixOfDouble(pvApiCtx,variableNameB,rowB,colB, B); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(cwritemat)(variableNameB, &rowB, &colB, B, strlen(variableNameB)); */ printf("\n"); printf("Display from Scilab of B:\n"); SendScilabJob("disp(B);"); /* Display B */ } /******************************** READ ****************************/ /* Load the previously set variable A */ { int rowA_ = 0,colA_ = 0,lp = 0; int i = 0, j = 0; double *matrixOfDouble = NULL; char variableToBeRetrieved[]="A"; SciErr sciErr; /* First, retrieve the size of the matrix */ sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrieved, &rowA_, &colA_, NULL); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(cmatptr)(variableToBeRetrieved, &rowA_, &colA_, &lp, strlen(variableToBeRetrieved)); */ /* Alloc the memory */ matrixOfDouble=(double*)malloc((rowA_*colA_)*sizeof(double)); /* Load the matrix */ sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrieved, &rowA_, &colA_, matrixOfDouble); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(creadmat)(variableToBeRetrieved,&rowA_,&colA_,matrixOfDouble,strlen(variableToBeRetrieved) ); */ printf("\n"); printf("Display from A (size: %d, %d):\n", rowA_, colA_); for(i=0; i < rowA_*colA_; i++) { fprintf(stdout,"A[%d] = %5.2f\n",i,matrixOfDouble[i]); } if (matrixOfDouble) { free(matrixOfDouble); matrixOfDouble=NULL; } } /* Load the previously set variable B */ { int rowB_ = 0, colB_ = 0, lp_ = 0; double *matrixOfDoubleB = NULL; int i = 0, j = 0; char variableToBeRetrievedB[] = "B"; SciErr sciErr; /* First, retrieve the size of the matrix */ sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, NULL); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(cmatptr)(variableToBeRetrievedB, &rowB_, &colB_, &lp_, strlen(variableToBeRetrievedB)); */ /* Alloc the memory */ matrixOfDoubleB = (double*)malloc((rowB_*colB_)*sizeof(double)); /* Load the matrix */ sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, matrixOfDoubleB); if(sciErr.iErr) { printError(&sciErr, 0); } /* * Prior to Scilab 5.2: * C2F(creadmat)(variableToBeRetrievedB,&rowB_,&colB_,matrixOfDoubleB,strlen(variableToBeRetrievedB) ); */ printf("\n"); printf("Display from B raw (size: %d, %d):\n",rowB_, colB_); for(i=0; i < rowB_*colB_; i++) { /* Display the raw matrix */ fprintf(stdout,"B[%d] = %5.2f\n",i,matrixOfDoubleB[i]); } printf("\n"); printf("Display from B formated (size: %d, %d):\n",rowB_, colB_); for(j = 0 ; j < rowB_ ; j++) { for(i = 0 ; i < colB_ ; i++) { /* Display the formated matrix ... the way the user * expect */ printf("%5.2f ",matrixOfDoubleB[i * rowB_ + j]); } printf("\n"); /* New row of the matrix */ } if (matrixOfDoubleB) { free(matrixOfDoubleB); matrixOfDoubleB=NULL; } } if ( TerminateScilab(NULL) == FALSE ) { fprintf(stderr,"Error while calling TerminateScilab\n"); return -2; } }
/*--------------------------------------------------------------------------*/ int SendScilabJob(char *job) { SciErr sciErr; int retCode = -1; char *command = NULL; #define COMMAND_EXECSTR "Err_Job = execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;" #define COMMAND_CLEAR "clear TMP_EXEC_STRING;clear Err_Job;quit;" if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STOP) { fprintf(stderr, "Error: SendScilabJob call_scilab engine not started.\n"); return retCode; } command = strdup(job); if (command) { double Err_Job = 0.; int m = 0, n = 0; /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun) (COMMAND_CLEAR, (long int)strlen(COMMAND_CLEAR)); SetLastJob(command); /* Creation of a temp variable in Scilab which contains the command */ sciErr = createNamedMatrixOfString(pvApiCtx, "TMP_EXEC_STRING", 1, 1, &command); if (sciErr.iErr) { printError(&sciErr, 0); /* Problem */ fprintf(stderr, "Error: SendScilabJob (1) call_scilab failed to create the temporary variable 'TMP_EXEC_STRING'.\n"); retCode = -1; if (command) { FREE(command); command = NULL; } return retCode; } /* Run the command within an execstr */ C2F(scirun) (COMMAND_EXECSTR, (long int)strlen(COMMAND_EXECSTR)); sciErr = getNamedVarDimension(pvApiCtx, "Err_Job", &m, &n); if (sciErr.iErr) { printError(&sciErr, 0); fprintf(stderr, "Error: SendScilabJob (2) call_scilab failed to detect the temporary variable 'Err_Job' size.\n"); retCode = -2; if (command) { FREE(command); command = NULL; } return retCode; } if ((m != 1) && (n != 1)) { fprintf(stderr, "Error: SendScilabJob (3) call_scilab detected a badly formated 'Err_Job' variable. Size [1,1] expected.\n"); retCode = -3; if (command) { FREE(command); command = NULL; } return retCode; } sciErr = readNamedMatrixOfDouble(pvApiCtx, "Err_Job", &m, &n, &Err_Job); if (sciErr.iErr) { printError(&sciErr, 0); fprintf(stderr, "Error: SendScilabJob (4) call_scilab failed to read the temporary variable 'Err_Job'.\n"); retCode = -4; if (command) { FREE(command); command = NULL; } return retCode; } if (command) { FREE(command); command = NULL; } retCode = (int)Err_Job; /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun) (COMMAND_CLEAR, (long int)strlen(COMMAND_CLEAR)); } else { fprintf(stderr, "Error: SendScilabJob (5) call_scilab failed to create the 'command' variable (MALLOC).\n"); retCode = -4; } return retCode; }