00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include "mat.h"
00027
00028 #define BUFSIZE 256
00029
00030 int main() {
00031 MATFile *pmat;
00032 mxArray *pa1, *pa2, *pa3;
00033 double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
00034 const char *file = "mattest.mat";
00035 char str[BUFSIZE];
00036 int status;
00037
00038 printf("Creating file %s...\n\n", file);
00039 pmat = matOpen(file, "w");
00040 if (pmat == NULL) {
00041 printf("Error creating file %s\n", file);
00042 printf("(Do you have write permission in this directory?)\n");
00043 return(EXIT_FAILURE);
00044 }
00045
00046 pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
00047 if (pa1 == NULL) {
00048 printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
00049 printf("Unable to create mxArray.\n");
00050 return(EXIT_FAILURE);
00051 }
00052
00053 pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
00054 if (pa2 == NULL) {
00055 printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
00056 printf("Unable to create mxArray.\n");
00057 return(EXIT_FAILURE);
00058 }
00059 memcpy((void *)(mxGetPr(pa2)), (void *)data, sizeof(data));
00060
00061 pa3 = mxCreateString("MATLAB: the language of technical computing");
00062 if (pa3 == NULL) {
00063 printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
00064 printf("Unable to create string mxArray.\n");
00065 return(EXIT_FAILURE);
00066 }
00067
00068 status = matPutVariable(pmat, "LocalDouble", pa1);
00069 if (status != 0) {
00070 printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
00071 return(EXIT_FAILURE);
00072 }
00073
00074 status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
00075 if (status != 0) {
00076 printf("Error using matPutVariableAsGlobal\n");
00077 return(EXIT_FAILURE);
00078 }
00079
00080 status = matPutVariable(pmat, "LocalString", pa3);
00081 if (status != 0) {
00082 printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
00083 return(EXIT_FAILURE);
00084 }
00085
00086
00087
00088
00089
00090
00091 memcpy((void *)(mxGetPr(pa1)), (void *)data, sizeof(data));
00092 status = matPutVariable(pmat, "LocalDouble", pa1);
00093 if (status != 0) {
00094 printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
00095 return(EXIT_FAILURE);
00096 }
00097
00098
00099 mxDestroyArray(pa1);
00100 mxDestroyArray(pa2);
00101 mxDestroyArray(pa3);
00102
00103 if (matClose(pmat) != 0) {
00104 printf("Error closing file %s\n",file);
00105 return(EXIT_FAILURE);
00106 }
00107
00108
00109
00110
00111 pmat = matOpen(file, "r");
00112 if (pmat == NULL) {
00113 printf("Error reopening file %s\n", file);
00114 return(EXIT_FAILURE);
00115 }
00116
00117
00118
00119
00120 pa1 = matGetVariable(pmat, "LocalDouble");
00121 if (pa1 == NULL) {
00122 printf("Error reading existing matrix LocalDouble\n");
00123 return(EXIT_FAILURE);
00124 }
00125 if (mxGetNumberOfDimensions(pa1) != 2) {
00126 printf("Error saving matrix: result does not have two dimensions\n");
00127 return(EXIT_FAILURE);
00128 }
00129
00130 pa2 = matGetVariable(pmat, "GlobalDouble");
00131 if (pa2 == NULL) {
00132 printf("Error reading existing matrix GlobalDouble\n");
00133 return(EXIT_FAILURE);
00134 }
00135 if (!(mxIsFromGlobalWS(pa2))) {
00136 printf("Error saving global matrix: result is not global\n");
00137 return(EXIT_FAILURE);
00138 }
00139
00140 pa3 = matGetVariable(pmat, "LocalString");
00141 if (pa3 == NULL) {
00142 printf("Error reading existing matrix LocalString\n");
00143 return(EXIT_FAILURE);
00144 }
00145
00146 status = mxGetString(pa3, str, sizeof(str));
00147 if(status != 0) {
00148 printf("Not enough space. String is truncated.");
00149 return(EXIT_FAILURE);
00150 }
00151 if (strcmp(str, "MATLAB: the language of technical computing")) {
00152 printf("Error saving string: result has incorrect contents\n");
00153 return(EXIT_FAILURE);
00154 }
00155
00156
00157 mxDestroyArray(pa1);
00158 mxDestroyArray(pa2);
00159 mxDestroyArray(pa3);
00160
00161 if (matClose(pmat) != 0) {
00162 printf("Error closing file %s\n",file);
00163 return(EXIT_FAILURE);
00164 }
00165 printf("Done\n");
00166 return(EXIT_SUCCESS);
00167 }
00168
00169
00170
00171