1、首先你得有matlab的擴充庫,不如安裝一個matlab。
2、其次,開啟Visual Studio,在"工具">"選項"中的"項目和解決方案"中的"VC++目錄",然後設定:
包含檔案:MATLAB 根目錄/R2008a/extern/include
庫檔案:MATLAB 根目錄/R2008a/extern/lib/win32/microsoft
3、在工程上單擊右鍵,選擇"屬性",在連接器中的輸入中寫上"libmx.lib libmat.lib libeng.lib",確定。
4、之後就可以變成了。主要函數產看matlab協助。
以下是我對Matlab不成熟的封裝:
cls_mx_matrix是對matlab變數的封裝。
cls_mx是對引擎的封裝。
class cls_mx_matrix<br />{<br />private:<br />mxArray* data;<br />double* p_data;<br />int dim_n;<br />int dim_m;<br />string str_name;<br />void reset(mxArray* array)<br />{<br />mxDestroyArray(data);<br />data = array;<br />p_data = (double*)mxGetData(data);<br />}<br />public:<br />int get_dim_n()<br />{<br />return dim_n;<br />}<br />int get_dim_m()<br />{<br />return dim_m;<br />}<br />void resize(int a_n,int a_m)<br />{<br />mxDestroyArray(data);<br />dim_n = a_n;<br />dim_m = a_m;<br />data = mxCreateDoubleMatrix(dim_n,dim_m,mxREAL);<br />p_data = (double*)mxGetData(data);<br />}<br />void get_subscript(cls_mx_matrix& that,int pos_i_begin,int pos_i_end,int pos_j_begin,int pos_j_end)<br />{<br />int len_i = pos_i_end - pos_i_begin + 1;<br />int len_j = pos_j_end - pos_j_begin + 1;<br />that.resize(len_i,len_j);<br />for(int cnt_i=pos_i_begin;cnt_i<=pos_i_end;cnt_i++)<br />{<br />for(int cnt_j=pos_j_begin;cnt_j<=pos_j_end;cnt_j++)<br />{<br />that[make_pair(cnt_i - pos_i_begin,cnt_j - pos_j_begin)] = (*this)[make_pair(cnt_i,cnt_j)];<br />}<br />}<br />}<br />void operator =(const cls_mx_matrix &that)<br />{<br />data = that.data;<br />p_data = that.p_data;<br />dim_n = that.dim_n;<br />dim_m = that.dim_m;<br />}<br />cls_mx_matrix(string a_str_name,int a_n,int a_m)<br />{<br />str_name = a_str_name;<br />dim_n = a_n;<br />dim_m = a_m;<br />data = mxCreateDoubleMatrix(dim_n,dim_m,mxREAL);<br />p_data = (double*)mxGetData(data);<br />}<br />cls_mx_matrix(mxArray* array)<br />{<br />mxDestroyArray(data);<br />data = array;<br />p_data = (double*)mxGetData(data);<br />}<br />double& operator[] (pair<int,int> pos)<br />{<br />return p_data[pos.first*dim_m + pos.second];<br />}<br />~cls_mx_matrix()<br />{<br />mxDestroyArray(data);<br />}<br />friend class cls_mx;<br />};<br />class cls_mx<br />{<br />private:<br />Engine* eng;<br />public:<br />cls_mx()<br />{<br />eng = engOpen(NULL);<br />}<br />void set_var(cls_mx_matrix& mx_mat)<br />{<br />engPutVariable(eng,mx_mat.str_name.c_str(),mx_mat.data);<br />}<br />cls_mx_matrix get_var(string name)<br />{<br />return cls_mx_matrix(engGetVariable(eng,name.c_str()));<br />}<br />void get_var(cls_mx_matrix& that)<br />{<br />that.reset(engGetVariable(eng,that.str_name.c_str()));<br />}<br />void command(string str)<br />{<br />engEvalString(eng,str.c_str());<br />}<br />~cls_mx()<br />{<br />engClose(eng);<br />}<br />};