作者:
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);
也是行不通
沒有很懂這程式,所以有點不知道該從哪裡下手
謝謝