Filter löschen
Filter löschen

MATPOWER dll to use in C

2 Ansichten (letzte 30 Tage)
Sk Subrina Shawlin
Sk Subrina Shawlin am 4 Jul. 2022
Bearbeitet: James Tursa am 9 Okt. 2023
I have created dll of matlab function makeSbus using matlab compiler sdk to use in C. I have the function dll, lib and necessary c code generated by the compiler. The function takes 1 integer and 2 double matrix as input and give a complex double matrix as output. It creates sparse matrices during processing in MATLAB. This is my code
mxArray* Bus, * Gen,*var;
mxArray* sbus = NULL;
Bus = mxCreateDoubleMatrix(9, 17, mxREAL);
Gen = mxCreateDoubleMatrix(3, 25, mxREAL);
int data1[]={100};
double B[9][13], G[3][21];
// reads B and G matrix from a csv file
memcpy(mxGetPr(Bus), B, 9 * 13 * sizeof(double));
memcpy(mxGetPr(Gen), G, 3 * 21 * sizeof(double));
memcpy(mxGetPr(var), data1, sizeof(int));
if (!makeSbusInitialize())
{
fprintf(stderr, "Could not initialize the library.\n");
return -2;
}
else
{
/* Call the library function */
mlfMakeSbus(1, &sbus, var, Bus, Gen);
size_t i = 0, j = 0; /* loop index variables */
size_t r = 0, c = 0; /* variables to store the row and column length of the matrix */
double* data; /* variable to point to the double data stored within the mxArray */
/* Get the size of the matrix */
r = mxGetM(in);
c = mxGetN(in);
/* Get a pointer to the double data in mxArray */
data = mxGetPr(in);
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
{
printf("%4.2f\t", data[j * c + i]);
}
printf("\n");
}
printf("\n");
makeSbusTerminate();
}
when I compile it, it shows "Error using sparse Index into matrix must be positive" .
Any idea how to debug it? Also, if I get a complex matrix as my output from matlab function, how to read it from C? Is the approach I'm taking here right? Any suggestion will be appreciated.
  1 Kommentar
James Tursa
James Tursa am 9 Okt. 2023
Bearbeitet: James Tursa am 9 Okt. 2023
What is var? What is in? Which variable is supposed to be sparse? Which variable might be complex? What version of MATLAB are you using?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ayush Gupta
Ayush Gupta am 9 Okt. 2023
The error "Error using sparse Index into matrix must be positive" indicates that there is an issue with the indices used to access elements in a sparse matrix. To debug this error, you can try the following steps:
1. Verify the inputs: Make sure that the input matrices you are passing to the MATLAB function are correctly initialized and have valid values. Check if any of the indices used to access elements are negative or zero.
2. Check the dimensions: Ensure that the dimensions of the input matrices are compatible with the MATLAB function's requirements. If the dimensions are incorrect, it can lead to accessing elements outside the valid range.
3. Look for any places where indices are used to access elements in the sparse matrix. Check if there are any potential issues such as off-by-one errors or incorrect calculations that may result in negative or zero indices.
4. After calling the mlfMakeSbus function, you are attempting to retrieve the output data from "in", but "in" is not defined in your code. You should use "sbus" instead, as that is the output mxArray from the "mlfMakeSbus" function call. Replace "in" with "sbus" in the code snippet you provided.
Hope this helps!

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by