[问题] c 读取 matlab .mat 档

楼主: totemist (totem)   2015-05-04 21:34:43
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Dev-C++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
将matlab存的.mat档 能写入C里面
喂入的资料(Input):
point.mat
预期的正确结果(Expected Output):
C能读到.mat
错误结果(Wrong Output):
stdafx.h : No such file or directory
mat.h : No such file or directory
matlab.h : No such file or directory
'MATFile' undeclared (first use this function)
'pmat' undeclared (first use this function)
'mxArray' undeclared (first use this function)
'pa' undeclared (first use this function)
'matOpen' undeclared (first use this function)
'MmatGetNextVariable' undeclared (first use this function)
'matGetNumberOfDimensions' undeclared (first use this function)
'mlfPrintMatrix' undeclared (first use this function)
'mxDestoryArray' undeclared (first use this function)
'matClose' undeclared (first use this function)
程式码(Code):(请善用置底文网页, 记得排版)
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "mat.h"
#include "matlab.h"
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libmatlb.lib")
#pragma comment(lib, "libmmfile.lib")
void main(int argc, char **argv)
{
MATFile *pmat;
const char* name=NULL;
mxArray *pa;
/* open mat file and read it's content */
pmat = matOpen("point.mat", "r");
if (pmat == NULL)
{
printf("Error Opening File: \"%s\"\n", argv[1]);
return;
}
/* Read in each array. */
pa = matGetNextVariable(pmat, &name);
while (pa!=NULL)
{
/*
* Diagnose array pa
*/
printf("\nArray %s has %d dimensions.", name,
mxGetNumberOfDimensions(pa));
//print matrix elements
mlfPrintMatrix(pa);
//get next variable
pa = matGetNextVariable(pmat,&name);
//destroy allocated matrix
mxDestroyArray(pa);
}
matClose(pmat);
}
补充说明(Supplement):
感觉好像是没有读到一些指令,是directory的问题吗?
像是stdafx.h mat.h matlab.h读不到
但我找到这3个在以下路径
C:\Program Files\MATLAB\R2012a\extern\include
所以是我该把我的.cpp档存到这个路径吗?
还是Dev-C++ 的路径要把这个加进去?
另外也用了这指令
#include "mat.h"
MATFile *matOpen(const char *point.mat, const char *r);
也是行不通
没有很懂这程式,所以有点不知道该从哪里下手
谢谢
作者: grayStone (灰色石头)   2015-05-05 05:22:00
档案拉出来放专案底下 或是include写绝对路径
作者: EdisonX (卡卡兽)   2015-05-05 08:25:00
应该是用 IDE 设 include 路径吧 .. 用楼上的方法有可能继续再拉一狗票的档案 (像 mat.h 可能有 1.h 2.h 3.h)
作者: grayStone (灰色石头)   2015-05-05 11:12:00
嗯…还是用楼上的方法好
作者: nz22556 (出门遇到鬼)   2015-05-07 03:53:00
可以在youtube打 connect c++ to matlab 有解说 不过平台是VS 应该有帮助

Links booklink

Contact Us: admin [ a t ] ucptt.com