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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* matcreat.cpp - MAT-file creation program
*
* See the MATLAB External Interfaces/API Guide for compiling information.
*
* Calling syntax:
*
* matcreat
*
* Create a MAT-file which can be loaded into MATLAB.
*
* This program demonstrates the use of the following functions:
*
* matClose
* matGetVariable
* matOpen
* matPutVariable
* matPutVariableAsGlobal
*
* Copyright 1984-2007 The MathWorks, Inc.
*/
#include <stdio.h>
#include <string.h> /* For strcmp() */
#include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
#include <vector> /* For STL */
#include "mat.h"
#define BUFSIZE 256
int main() {
MATFile *pmat;
mxArray *pa1, *pa2, *pa3;
std::vector<int> myInts;
myInts.push_back(1);
myInts.push_back(2);
printf("Accessing a STL vector: %d\n", myInts[1]);
double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
const char *file = "mattest.mat";
char str[BUFSIZE];
int status;
printf("Creating file %s...\n\n", file);
pmat = matOpen(file, "w");
if (pmat == NULL) {
printf("Error creating file %s\n", file);
printf("(Do you have write permission in this directory?)\n");
return(EXIT_FAILURE);
}
pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
if (pa1 == NULL) {
printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
printf("Unable to create mxArray.\n");
return(EXIT_FAILURE);
}
pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
if (pa2 == NULL) {
printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
printf("Unable to create mxArray.\n");
return(EXIT_FAILURE);
}
memcpy((void *)(mxGetPr(pa2)), (void *)data, sizeof(data));
pa3 = mxCreateString("MATLAB: the language of technical computing");
if (pa3 == NULL) {
printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
printf("Unable to create string mxArray.\n");
return(EXIT_FAILURE);
}
status = matPutVariable(pmat, "LocalDouble", pa1);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
return(EXIT_FAILURE);
}
status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
if (status != 0) {
printf("Error using matPutVariableAsGlobal\n");
return(EXIT_FAILURE);
}
status = matPutVariable(pmat, "LocalString", pa3);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
return(EXIT_FAILURE);
}
/*
* Ooops! we need to copy data before writing the array. (Well,
* ok, this was really intentional.) This demonstrates that
* matPutVariable will overwrite an existing array in a MAT-file.
*/
memcpy((void *)(mxGetPr(pa1)), (void *)data, sizeof(data));
status = matPutVariable(pmat, "LocalDouble", pa1);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
return(EXIT_FAILURE);
}
/* clean up */
mxDestroyArray(pa1);
mxDestroyArray(pa2);
mxDestroyArray(pa3);
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",file);
return(EXIT_FAILURE);
}
/*
* Re-open file and verify its contents with matGetVariable
*/
pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error reopening file %s\n", file);
return(EXIT_FAILURE);
}
/*
* Read in each array we just wrote
*/
pa1 = matGetVariable(pmat, "LocalDouble");
if (pa1 == NULL) {
printf("Error reading existing matrix LocalDouble\n");
return(EXIT_FAILURE);
}
if (mxGetNumberOfDimensions(pa1) != 2) {
printf("Error saving matrix: result does not have two dimensions\n");
return(EXIT_FAILURE);
}
pa2 = matGetVariable(pmat, "GlobalDouble");
if (pa2 == NULL) {
printf("Error reading existing matrix GlobalDouble\n");
return(EXIT_FAILURE);
}
if (!(mxIsFromGlobalWS(pa2))) {
printf("Error saving global matrix: result is not global\n");
return(EXIT_FAILURE);
}
pa3 = matGetVariable(pmat, "LocalString");
if (pa3 == NULL) {
printf("Error reading existing matrix LocalString\n");
return(EXIT_FAILURE);
}
status = mxGetString(pa3, str, sizeof(str));
if(status != 0) {
printf("Not enough space. String is truncated.");
return(EXIT_FAILURE);
}
if (strcmp(str, "MATLAB: the language of technical computing")) {
printf("Error saving string: result has incorrect contents\n");
return(EXIT_FAILURE);
}
/* clean up before exit */
mxDestroyArray(pa1);
mxDestroyArray(pa2);
mxDestroyArray(pa3);
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",file);
return(EXIT_FAILURE);
}
printf("Done\n");
return(EXIT_SUCCESS);
} |
Partager