0

Hi 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 matlab provided api. Is there any way to do it in simple C without any api?

Update: I just tried with 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 from extern/include/mat.h directory,including matrix.h and tmwtypes.h.

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 spec.

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

dsolimano
  • 8,600
  • 3
  • 47
  • 62
  • 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
  • @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