Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading/Writing MAT files in C

308 views
Skip to first unread message

Jerome

unread,
May 11, 2012, 11:51:31 AM5/11/12
to
HI,
I am trying to compile the following code, but am unsuccessful. I have not seen any documentation of using the mat.h file. Any suggestions would be great

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

int diagnose(const char *file) {
MATFile *pmat;
const char **dir;
const char *name;
int ndir;
int i;
mxArray *pa;

printf("Reading file %s...\n\n", file);

/*
* * Open file to get directory
* */
pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return(1);
}

/*
* * get directory of MAT-file
* */
dir = (const char **)matGetDir(pmat, &ndir);
if (dir == NULL) {
printf("Error reading directory of file %s\n", file);
return(1);
} else {
printf("Directory of %s:\n", file);
for (i=0; i < ndir; i++)
printf("%s\n",dir[i]);
}
mxFree(dir);

/* In order to use matGetNextXXX correctly, reopen file to read in headers. */
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",file);
return(1);
}
pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error reopening file %s\n", file);
return(1);
}

/* Get headers of all variables */
printf("\nExamining the header for each variable:\n");
for (i=0; i < ndir; i++) {
pa = matGetNextVariableInfo(pmat, &name);
if (pa == NULL) {
printf("Error reading in file %s\n", file);
return(1);
}
/* Diagnose header pa */
printf("According to its header, array %s has %d dimensions\n",
name, mxGetNumberOfDimensions(pa));
if (mxIsFromGlobalWS(pa))
printf(" and was a global variable when saved\n");
else
printf(" and was a local variable when saved\n");
mxDestroyArray(pa);
}

/* Reopen file to read in actual arrays. */
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",file);
return(1);
}
pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error reopening file %s\n", file);
return(1);
}

/* Read in each array. */
printf("\nReading in the actual array contents:\n");
for (i=0; i<ndir; i++) {
pa = matGetNextVariable(pmat, &name);
if (pa == NULL) {
printf("Error reading in file %s\n", file);
return(1);
}
/*
* * Diagnose array pa
* */
printf("According to its contents, array %s has %d dimensions\n",
name, mxGetNumberOfDimensions(pa));
if (mxIsFromGlobalWS(pa))
printf(" and was a global variable when saved\n");
else
printf(" and was a local variable when saved\n");
mxDestroyArray(pa);
}

if (matClose(pmat) != 0) {
printf("Error closing file %s\n",file);
return(1);
}
printf("Done\n");
return(0);
}

int main(int argc, char **argv)
{

int result;

if (argc > 1)
result = diagnose(argv[1]);
else{
result = 0;
printf("Usage: matdgns <matfile>");
printf(" where <matfile> is the name of the MAT-file");
printf(" to be diagnosed\n");
}

return (result==0)?EXIT_SUCCESS:EXIT_FAILURE;

}

Ben

unread,
May 11, 2012, 12:01:11 PM5/11/12
to
"Jerome " <the_...@hotmail.com> wrote in message <jojci3$he4$1...@newscl01ah.mathworks.com>...
> HI,
> I am trying to compile the following code, but am unsuccessful. I have not seen any documentation of using the mat.h file. Any suggestions would be great
>
> #include <stdio.h>
> #include <stdlib.h>
> #include "mat.h"
>
I don't see
#include "engine.h"

I'm not sure, but it might be needed for some of your code.

James Tursa

unread,
May 11, 2012, 12:35:07 PM5/11/12
to
"Ben" wrote in message <jojd47$k7c$1...@newscl01ah.mathworks.com>...
engine.h would work since it includes matrix.h as a byproduct, but since I don't see any engine related functions in the code, what *will* be needed is matrix.h. You can get that by including engine.h or directly as:

#include "matrix.h"

The matrix.h file is needed in order to do all the mxArray manipulation.

James Tursa

Ben

unread,
May 11, 2012, 12:46:07 PM5/11/12
to
<jojci3$he4$1...@newscl01ah.mathworks.com>...
> > > HI,
> > > I am trying to compile the following code, but am unsuccessful. I have not seen any documentation of using the mat.h file. Any suggestions would be great

What errors are you getting? Any more specifics you can offer?

Jerome

unread,
May 11, 2012, 1:46:26 PM5/11/12
to
Thanks you! when I complied I received the following:
gcc -I/usr/local/include/ -L/usr/local/lib example2.c

example2.c:3:17: error: mat.h: No such file or directory
example2.c:4:20: error: engine.h: No such file or directory
example2.c: In function ‘diagnose’:
example2.c:6: error: ‘MATFile’ undeclared (first use in this function)
example2.c:6: error: (Each undeclared identifier is reported only once
example2.c:6: error: for each function it appears in.)
example2.c:6: error: ‘pmat’ undeclared (first use in this function)
example2.c:11: error: ‘mxArray’ undeclared (first use in this function)
example2.c:11: error: ‘pa’ undeclared (first use in this function)
example2.c:27: warning: cast to pointer from integer of different size




"Ben" wrote in message <jojfof$2mh$1...@newscl01ah.mathworks.com>...

dpb

unread,
May 11, 2012, 2:28:46 PM5/11/12
to
On 5/11/2012 12:46 PM, Jerome wrote:
> Thanks you! when I complied I received the following:
> gcc -I/usr/local/include/ -L/usr/local/lib example2.c
> example2.c:3:17: error: mat.h: No such file or directory
> example2.c:4:20: error: engine.h: No such file or directory
...

The compiler didn't find the above header files--

--

Jerome

unread,
May 11, 2012, 2:52:29 PM5/11/12
to
Thanks,
After locating the header files, I received the following error.
gcc -I/usr/local/MATLAB/R2012a/extern/include/ -L/usr/local/MATLAB/R2012/extern/lib example2.c -lm
/tmp/ccmBsgpB.o: In function `diagnose':
example2.c:(.text+0x32): undefined reference to `matOpen'
example2.c:(.text+0x73): undefined reference to `matGetDir'
example2.c:(.text+0xf3): undefined reference to `mxFree'
example2.c:(.text+0xff): undefined reference to `matClose'
example2.c:(.text+0x137): undefined reference to `matOpen'
example2.c:(.text+0x18e): undefined reference to `matGetNextVariableInfo'
example2.c:(.text+0x1c8): undefined reference to `mxGetNumberOfDimensions'
example2.c:(.text+0x1f0): undefined reference to `mxIsFromGlobalWS'
example2.c:(.text+0x216): undefined reference to `mxDestroyArray'
example2.c:(.text+0x232): undefined reference to `matClose'
example2.c:(.text+0x26a): undefined reference to `matOpen'
example2.c:(.text+0x2c1): undefined reference to `matGetNextVariable'
example2.c:(.text+0x2fb): undefined reference to `mxGetNumberOfDimensions'
example2.c:(.text+0x323): undefined reference to `mxIsFromGlobalWS'
example2.c:(.text+0x349): undefined reference to `mxDestroyArray'
example2.c:(.text+0x365): undefined reference to `matClose'
collect2: ld returned 1 exit status

Thanks!
dpb <no...@non.net> wrote in message <jojlor$n7e$1...@speranza.aioe.org>...

dpb

unread,
May 11, 2012, 3:23:28 PM5/11/12
to
On 5/11/2012 1:52 PM, Jerome wrote:

...don't toppost...

> Thanks,
> After locating the header files, I received the following error.
...

> example2.c:(.text+0x32): undefined reference to `matOpen'
...

well, now the linker needs to know where the librar(y|ies) is/are...

--

Jerome

unread,
May 11, 2012, 3:52:16 PM5/11/12
to
dpb <no...@non.net> wrote in message <jojovd$vg0$1...@speranza.aioe.org>...
Thanks,
I can compile and link using mex instead of gcc but it gives a seg fault. This should not be. Any suggestions?

dpb

unread,
May 11, 2012, 4:03:37 PM5/11/12
to
On 5/11/2012 2:52 PM, Jerome wrote:
> dpb <no...@non.net> wrote in message <jojovd$vg0$1...@speranza.aioe.org>...
...

> I can compile and link using mex instead of gcc but it gives a seg
> fault. This should not be. Any suggestions?

Then you're down to debugging...this generally means a bad pointer
somewhere--you've got a reference to a passed by value argument in place
of a by reference or vice versa, an array index 0 or out of bounds,
etc., etc., etc, ...

--

0 new messages