0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • if you get the sources from python libs reading matlab, you could get the code directly. – Jean-François Fabre Jan 25 '18 at 16:24
  • @dsolimano, I tried with the api but now it seems functions are not recognized. –  Jan 25 '18 at 16:32
  • @Asparagus I think, perhaps, you need to review how to use libraries in C. You will need to include the header file and probably pass the Matlab header directory to your compile, and also link the matlab library into your binary. – dsolimano Jan 25 '18 at 16:56
  • @ dsolimano probaly you mean like this: "#include "/usr/local/MATLAB/R2011b/extern/include/mat.h" ? –  Jan 25 '18 at 17:00
  • 1
    @Asparagus not sure about your IDE but https://stackoverflow.com/questions/6016815/how-to-include-needed-c-library-using-gcc is a pointer in the right direction for GCC. You need to do two separate things, 1, include the header file, and 2, link in the library. Looks like you're missing 2. – dsolimano Jan 25 '18 at 19:27