-
Notifications
You must be signed in to change notification settings - Fork 0
/
matdgns.c
144 lines (129 loc) · 3.2 KB
/
matdgns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* MAT-file diagnose program
*
* See the MATLAB API Guide for compiling information.
*
* Calling syntax:
*
* matdgns <matfile>
*
* It will diagnose the MAT-file named <matfile>.
*
* This program demonstrates the use of the following functions:
*
* matClose
* matGetDir
* matGetNextVariable
* matGetNextVariableInfo
* matOpen
*
* Copyright 1984-2003 The MathWorks, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include "libs/matlab/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;
}