Problems with the Matlab C API, matOpen applied to structures

조회 수: 5 (최근 30일)
Kyle
Kyle 2013년 5월 16일
I'm having some troubles writing c-code to read a saved .mat file containing a structure. I have code that calls the subfunction:
--------------------------------------
const char *filePtr = "OpenMATLABTest.mat";
printf("File Location: %s \n\n", filePtr);
int statusLoad = CryoSimLoad(filePtr);
printf("Status is: %i \n\n", statusLoad);
--------------------------------------
and the subfunction itself:
--------------------------------------
/* CryoSimLoad.c
*/
#include <stdio.h>
#include <math.h>
#include "mat.h"
#include "CryosimSimple.h" /* Model's header file */
#include "rtwtypes.h" /* MathWorks types */
#define BUFSIZE 256
int CryoSimLoad() {
MATFile *mfPtr; /* MAT-file pointer */
mxArray *aPtr; /* mxArray pointer */
/* */
mfPtr = matOpen("OpenMATLABTest.mat", "r");
if (mfPtr == NULL) {
printf("Error opening file \n");
return(1);
}
if (matClose(mfPtr) != 0) {
printf("Error closing file\n");
return(EXIT_FAILURE);
}
mxDestroyArray(aPtr);
return(0);
}
------------------------------------------------------------------
when there is a structure in the saved file I get a : "RUN FAILED (exit value 1)" , if there isn't a structure (just arrays and scalars) it loads.
Does anyone have any suggestions?
  댓글 수: 2
James Tursa
James Tursa 2013년 5월 16일
편집: James Tursa 2013년 5월 16일
1) You call with an argument
CryoSimLoad(filePtr)
but the function doesn't have any arguments
int CryoSimLoad() { etc
2) I don't see a RUN FAILED message string in your code, so I don't know exactly where you think the error is happening.
3) You call mxDestroyArray(aPtr) with an uninitialized pointer argument, so I would expect this to bomb.
I suspect you did some code snipping and simplification for posting purposes, but you have cut out too much for us to see what is going on and introduced errors that are probably not there in your actual code. Please edit your post to correct this.
Kyle
Kyle 2013년 5월 16일
#include <stdio.h>
#include <math.h>
#include "mat.h"
#include "CryosimSimple.h" /* Model's header file */
#include "rtwtypes.h" /* MathWorks types */
#define BUFSIZE 256
static void
analyzestructarray(const mxArray *sPtr, const char *fName)
{
mwSize nElements; /* number of elements in array */
mwIndex eIdx; /* element index */
const mxArray *fPtr; /* field pointer */
double *realPtr; /* pointer to data */
double total; /* value to calculate */
total = 0;
nElements = (mwSize)mxGetNumberOfElements(sPtr);
for (eIdx = 0; eIdx < nElements; eIdx++) {
fPtr = mxGetField(sPtr, eIdx, fName);
if ((fPtr != NULL)
&& (mxGetClassID(fPtr) == mxDOUBLE_CLASS)
&& (!mxIsComplex(fPtr)))
{
realPtr = mxGetPr(fPtr);
total = total + realPtr[0];
}
}
printf("Total for %s: %.2f\n", fName, total);
}
int CryoSimLoad() {
MATFile *mfPtr; /* MAT-file pointer */
mxArray *aPtr; /* mxArray pointer */
const char *arrControlPtr = "controlSignals";
/* */
const char *filePtr = "OpenMATLABTest.mat";
mfPtr = matOpen(filePtr, "r");
if (mfPtr == NULL) {
printf("Error opening file \n");
return(1);
}
aPtr = matGetVariable(mfPtr, arrControlPtr);
if (aPtr == NULL) {
printf("mxArray not found: %s\n", arrControlPtr);
return(1);
}
if (mxGetClassID(aPtr) == mxSTRUCT_CLASS) {
size_t intStructureSize = (size_t) mxGetNumberOfFields(aPtr);
int i;
for (i = 0; i< intStructureSize; i++){
const char tmpFieldName = *mxGetFieldNameByNumber(aPtr, i);
const char *tmpFieldNamePtr = &tmpFieldName;
if (mxGetFieldNumber(aPtr, tmpFieldNamePtr) == -1) {
printf("Field not found: %s\n", tmpFieldName);
}
else {
analyzestructarray(aPtr, tmpFieldNamePtr);
}
}
}
else {
printf("%s is not a structure\n", aPtr);
}
if (matClose(mfPtr) != 0) {
printf("Error closing file\n");
return(EXIT_FAILURE);
}
mxDestroyArray(aPtr);
return(0);
}
Where the structure input is as follows: controlSignals = struct('time', 0, 'CV001cmd', 0, 'CV002cmd', 0);
and has been saved in the OpenMATLABTest.mat
I am working out of Netbeans 7.2, with MATLAB .dlls from version 2013a (64bit), and the C compiler x86_64-w64-mingw32-gcc-4.5.3 available via Cygwin. Note Netbeans compiles the code, but when run, the "RUN FAILED (exit value 1)" results as the :only: output (despite print statements prior to call). This error only occurs when I have structures in the saved file, I have compiled and run the code as written successfully with double, strings, and arrays.
thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by