外部インタフェース/API | ![]() ![]() |
MAT-ファイルをCで読み込む
この例題プログラムは、MAT-ファイルの読み込みや診断を行うライブラリルーチンの使用法を記述します。
/* $ Revision: 1.1 $ */ /* * MAT-file diagnose program * * Calling syntax: * * matdgns <matfile.mat> * * It diagnoses the MAT-file named <matfile.mat>. * * This program demonstrates the use of the following functions: * * matClose * matGetDir * matGetNextArray * matGetNextArrayHeader * matOpen * * Copyright (c) 1984-1998 The MathWorks, Inc. */ #include <stdio.h> #include <stdlib.h> #include "string.h" #include "mat.h" int diagnose(const char *file) { MATFile*pmat; char**dir; intndir; inti; 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 = 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 = matGetNextArrayHeader(pmat); 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", mxGetName(pa), 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 = matGetNextArray(pmat); 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", mxGetName(pa), 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"); } return (result==0)?EXIT_SUCCESS:EXIT_FAILURE; }
このプログラムをコンパイル、リンクした後で、結果を見ることができます。
matdgns mattest.mat Reading file mattest.mat... Directory of mattest.mat: GlobalDouble LocalString LocalDouble Examining the header for each variable: According to its header, array GlobalDouble has 2 dimensions and was a global variable when saved According to its header, array LocalString has 2 dimensions and was a local variable when saved According to its header, array LocalDouble has 2 dimensions and was a local variable when saved Reading in the actual array contents: According to its contents, array GlobalDouble has 2 dimensions and was a global variable when saved According to its contents, array LocalString has 2 dimensions and was a local variable when saved According to its contents, array LocalDouble has 2 dimensions and was a local variable when saved Done
![]() | MAT-ファイルの例 | MAT-ファイルをFortranで作成 | ![]() |