To access a Scilab matrix from a C interface, you must access it columnwise.
Here is the C interface which is accessing the matrix transmitted has the first argument of the function test_matrix:
#include <api_scilab.h> #if (API_SCILAB_VERSION < 3) #include <stack-c.h> #endif #include <localization.h> #include <sciprint.h> #include <Scierror.h> int test_matrix(char * fname) { int *piAddressVarOne = NULL; int i = 0, j = 0; int m = 0, n = 0; double *dValues = NULL; CheckRhs(1, 1); CheckLhs(0, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (!isDoubleType(pvApiCtx, piAddressVarOne)) { Scierror(999, _("%s: Wrong type for input argument #%d.\n"), fname, 1); return 0; } sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m, &n , &dValues); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } for(i=0; i < m; i++) { for(j=0; j < n; j++) { sciprint("%f ", dValues[(j * m)+ i]); } sciprint("\n"); } return 0; }
Now, here is the builder.sce:
// This is the builder.sce // must be run from this directory lines(0); ilib_name = 'libtest_matrix'; files = ['test_matrix.c']; libs = []; table =['test_matrix', 'test_matrix']; ldflags = ""; cflags =""; fflags =""; // do not modify below // ---------------------------------------------- ilib_build(ilib_name,table,files,libs,[],ldflags,cflags,fflags)
And here is an example (don't forget to do 'exec loader.sce;' before executing this test):
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]; test_matrix(A);
Now, the result is:
-->exec loader.sce; Bibliothèque partagée chargée. Link done. -->exec ex_test_matrix.sce; 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000 15.000000