linux下 C++ 读取mat文件 MATLAB extern cyphon scipy 未完待续

 1.使用Matlab的C扩展,需要用户安装matlab。

g++ -L/media/exsoftware/MATLAB/R2013b/bin/glnxa64 -Wl,-rpath,/media/exsoftware/MATLAB/R2013b/bin/glnxa64 -o "test"  ./d.o ./testmat.o   -lmat -lmx

g++ compiler:

include:

/media/exsoftware/MATLAB/R2013b/extern/include

ECLIPSE SETTING:

 Linker

 library:

 -L /media/exsoftware/MATLAB/R2013b/bin/glnxa64

-lmat -lmx

miscellaneous, Linker flags:

-Wl,-rpath,/media/exsoftware/MATLAB/R2013b/bin/glnxa64

/*
 * 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.
 */
/* $Revision: 1.8.4.1 $ */
#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...

", file);

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

  /*
   * get directory of MAT-file
   */
  dir = (const char **)matGetDir(pmat, &ndir);
  if (dir == NULL) {
    printf("Error reading directory of file %s
", file);
    return(1);
  } else {
    printf("Directory of %s:
", file);
    for (i=0; i < ndir; i++)
      printf("%s
",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
",file);
    return(1);
  }
  pmat = matOpen(file, "r");
  if (pmat == NULL) {
    printf("Error reopening file %s
", file);
    return(1);
  }

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

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

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

  if (matClose(pmat) != 0) {
      printf("Error closing file %s
",file);
      return(1);
  }
  printf("Done
");
  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;

}
View Code

2. 使用Python的scipy,或者scipy

 Now, you have a complete Python installation in your home directory. Pass -I /home/username/python/include to gcc when compiling to make it aware of Python.h. Pass -L /home/username/python/lib and -lpython2.7 when linking. 

原文地址:https://www.cnblogs.com/huashiyiqike/p/3937283.html