Dhruba Raj Joshi
Dhruba Raj Joshi

Reputation: 53

Read Matlab's .mat file in C

I am using Matlab to generate a large matrix, and I want to use it in C.

How can I read Matlab's .mat file in C?

If it is possible, please answer how?

Upvotes: 4

Views: 8493

Answers (3)

Leos313
Leos313

Reputation: 5627

matOpen (C)

C Syntax

#include "mat.h"
MATFile *matOpen(const char *filename, const char *mode);

filename Name of file to open.

mode File opening mode.

Here there are examples and explanations. Here there are all the link for MAT-file API. I recommend to read and study the examples.

Upvotes: 1

brm
brm

Reputation: 3796

Supposedly Matlab provides its own API to access such files from C: Read and write MAT files. I haven't used it myself, so I don't know how straightforward this is.

If you really want to access the binary data, a search engine came up with this PDF file, matfile_format.pdf, which describes the entire format. This is definitely not an easy solution.

You can easily read such files in Python however, see this topic. Reading a file this way and writing it again in a format that's easy for you to use in C seems like a good solution.

Upvotes: 3

Rafael Ferreira
Rafael Ferreira

Reputation: 21

If just text is enough...

#include <stdio.h>
#include <stdlib.h>

int main()
{

    FILE *pf;

    pf = fopen("something.m", "r");

    int buffer;

    while (buffer != EOF)
    {

        buffer = getc(pf);
        printf("%c", buffer);

    }



}

Upvotes: -2

Related Questions