从C读取.mat文件:可以读取变量;但无法正确返回

加里玛·辛格(Garima Singh)

我正在尝试使用MATLAB-API来使用C(不是C ++)读取.mat文件。

这是MATLAB代码,可以创建我想要的.mat文件:

A = [[1 2 3]; [5 7 1]; [3 5 9]];
B = [[2 4];[5 7]];
Creator = 'DKumar';

nFilters = 2;

Filters{1} = [[-1.0 -1.0 -1.0]; [-1.0 8 -1.0]; [-1.0 -1.0 -1.0]];
Filters{2} = 2.0*[[-1.0 -1.0 -1.0]; [-1.0 8 -1.0]; [-1.0 -1.0 -1.0]];

cd('/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File');
save('Test_FILE.mat', 'A', 'B', 'Creator', 'nFilters', 'Filters');

请注意,我还需要阅读单元结构或类似内容。

(1)在C代码中,看来我可以很好地读取存储在.mat中的矩阵;但是,无法正确返回(请参见最后的输出)。

(2)我仍然不知道单元结构,在此示例中,该单元将存储大小可能不同的双矩阵。

完整的C代码如下。首先是函数matread,该函数看似可以正确读取数据。

#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/MATLAB/R2011b/extern/include/mat.h"


struct stDoubleMat{
   double* pValueInField;
   int nRows, nCols;
};

void matread(const char *file, const char *FieldName2Read, struct stDoubleMat oDoubleMat_LOC)
{
    printf("Reading file %s...\n\n", file);

    //Open file to get directory
    MATFile* pmat = matOpen(file, "r");

    if (pmat == NULL) {
      printf("Error opening file %s\n", file);
      return;
    }

    // extract the specified variable
    mxArray *arr = matGetVariable(pmat, FieldName2Read);

    double *pr;
    if (arr != NULL && !mxIsEmpty(arr)) {
        // copy data
        mwSize num = mxGetNumberOfElements(arr);

        pr = mxGetPr(arr);

        if (pr != NULL) {
        oDoubleMat_LOC.pValueInField = pr;
            oDoubleMat_LOC.nRows  = mxGetM(arr);
            oDoubleMat_LOC.nCols  = mxGetN(arr);
        }
    printf("From inside the function \n") ;
        printf( "oDoubleMat_LOC.nRows %i ; oDoubleMat_LOC.nCols %i \n", oDoubleMat_LOC.nRows , oDoubleMat_LOC.nCols);

    }else{
        printf("nothing to read \n") ;
    }

    // cleanup
    mxDestroyArray(arr);
    matClose(pmat);

    return;
}

在同一个文件中,该main函数似乎无法返回读取的数据:

int main(int argc, char **argv)
{
    const char *FileName = "/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat";
    const char *FieldName2Read = "A";

    struct stDoubleMat oDoubleMat; 
    matread(FileName, FieldName2Read, oDoubleMat);
    double* v = oDoubleMat.pValueInField;


    printf("From main \n");
    printf( "oDoubleMat.nRows %i ; oDoubleMat.nCols %i \n", oDoubleMat.nRows , oDoubleMat.nCols);
/*
    for (int i = 0; i < oDoubleMat.nElements; i++)
    {
        std::cout <<" copied value : " << *v << "\n";
        v = v +1;
    }*/

    return 0;
}

这是输出

$ gcc -o Test Read_MatFile_DKU_2.c -I/usr/local/MATLAB/R2011b/extern/include -L/usr/local/MATLAB/R2011b/bin/glnxa64 -lmat -lmx

$ ./Test 
Reading file /home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat...

From inside the function 
oDoubleMat_LOC.nRows 3 ; oDoubleMat_LOC.nCols 3 
From main 
oDoubleMat.nRows 0 ; oDoubleMat.nCols 0 

更新:

这是读取矩阵字段就好的更新代码。我还有no clue about how to read "cell-structure"

#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/MATLAB/R2011b/extern/include/mat.h"

mxArray *arr;

struct stDoubleMat{
   double* pValueInField;
   int nRows, nCols;
};

void matread(const char *file, const char *FieldName2Read, struct stDoubleMat* poDoubleMat_LOC)
{
    printf("Reading file %s...\n\n", file);

    //Open file to get directory
    MATFile* pmat = matOpen(file, "r");

    if (pmat == NULL) {
      printf("Error opening file %s\n", file);
      return;
    }

    // extract the specified variable
    arr = matGetVariable(pmat, FieldName2Read);

    double *pr;
    if (arr != NULL && !mxIsEmpty(arr)) {
        // copy data
        mwSize num = mxGetNumberOfElements(arr);

        pr = mxGetPr(arr);

        if (pr != NULL) {
        poDoubleMat_LOC->pValueInField = pr;
            poDoubleMat_LOC->nRows  = mxGetM(arr);
            poDoubleMat_LOC->nCols  = mxGetN(arr);
        }
    printf("From inside the function \n") ;
        printf( "oDoubleMat_LOC.nRows %i ; oDoubleMat_LOC.nCols %i \n", poDoubleMat_LOC->nRows , poDoubleMat_LOC->nCols);

    }else{
        printf("nothing to read \n") ;
    }

    // close the file
    matClose(pmat);

    return;
}

int main(int argc, char **argv)
{
    const char *FileName = "/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat";
    const char *FieldName2Read = "A";

    struct stDoubleMat oDoubleMat; 
    matread(FileName, FieldName2Read, &oDoubleMat);
    double* v = oDoubleMat.pValueInField;


    printf("From main \n");
    printf( "oDoubleMat.nRows %i ; oDoubleMat.nCols %i \n", oDoubleMat.nRows , oDoubleMat.nCols);

    int i;
    for (i = 0; i < oDoubleMat.nCols*oDoubleMat.nRows; i++)
    {
        printf(" copied value : %f \n", *v);
        v = v +1;
    }

    // cleanup the mex-array
    mxDestroyArray(arr);

    return 0;
}
chapjc

您通过值将“输出”参数(oDoubleMat_LOC传递matread,因此您永远无法真正获得输出,因为它是在输入中复制的(即仅在本地修改):

void matread(const char *file, const char *FieldName2Read, 
    struct stDoubleMat oDoubleMat_LOC) /* oDoubleMat_LOC copied */

由于您使用的是C,因此引用不可用,因此可以传递一个指针。重新定义matread

void matread(const char *file, const char *FieldName2Read, 
    struct stDoubleMat *oDoubleMat_LOC) /* use a pointer */

然后在内部matread,您需要取消引用它以修改其字段(使用->代替.):

oDoubleMat_LOC->pValueInField = pr;
oDoubleMat_LOC->nRows  = mxGetM(arr);
oDoubleMat_LOC->nCols  = mxGetN(arr);

在中main,这样调用:

struct stDoubleMat oDoubleMat; 
matread(FileName, FieldName2Read, &oDoubleMat);

但是,请注意,您遇到更大的问题,因为mxArrayback的支持double *pValueInField是在内部 分配和销毁的matread虽然您可以将指针返回到数据数组,但它将是一个悬空指针,该指针指向已释放的数据。您将需要分配一个mxArray外部matread并将其传入,或分配一个double *并将数据复制到内部matread否则,一mxDestroyArray经调用,指针就无用了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章