user8126672
user8126672

Reputation:

Read .mat file in C

I am converting MATLAB code to C. During that process I need to read some .mat file. Read the .mat file field, save them into array then process it.

I have seen some example here.

Here they have used the MATLAB-provided API. Is there a way to do it in simple C without any API?


I tried with the API according to suggestion with simple code:

#include "mat.h"

void matread_Cell(const char *file, const char *FieldName, int CellIndex)
{
    printf("\n From matread_cell Reading matfile %s...\n\n", file);
    MATFile* pmat = matOpen(file, "r");

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

Unfortunately, it does not recognize MATFile or matOpen. The error says

undefined reference to `matOpen' Blockquote

I copied the mat.h file from the extern/include/mat.h directory, including matrix.h and tmwtypes.h.

Upvotes: 0

Views: 1008

Answers (1)

dsolimano
dsolimano

Reputation: 9006

MATLAB documents their file format. If you had a lot of time on your hands, you could rewrite your own parser from the specification.

But, I would say the API is simple C, and doing it without the API is the complicated way of doing it.

Upvotes: 1

Related Questions