Use matrix c\c++ API with row-based indexing
이전 댓글 표시
Hi all, in my project I perform some calculations in c++, and then I am using Matrix API (mxCreateNumericArray) to save my 2D array into .mat file. In c++ I work with native row-based indexing, however Matlab functions assume column-based order. Is there an option to quickly fix this? I can convert row-based to column based, but don't want to do this (for greater performance). Btw, my array is not symmetric. Here is a piece of c++ code:
const mwSize dims[] = { M1, M2 };
mxArray *pa1 = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxREAL);
memcpy((void *)(mxGetPr(pa1)), (void *)iu_cpu, M1*M2*sizeof(float));
matPutVariable(pmat, "LocalDouble", pa1);
mxDestroyArray(pa1);
So, iu_cpu is 1D array representing 2D matrix in row-based order. And matlab 2D array pa1 is obtained from iu_cpu assuming column-based order.
Here is simple example: Imagine I have matrix
a={1 2 3
4 5 6
7 8 9
10 11 12}
Then, iu_cpu={1,2,3,4,5,6,7,8,9,10,11,12}. However array pa1 will be obtained from iu_cpu assuming column-major order: pa1={1,4,7,10,2,5,8,11,3,6,9,12}
Update: Following the advice of James Tursa, I tried to use mexCallMATLAB. For some reason, Unhandled expression:access violation error appears when I am tring to call this function. Here is full code:
#include "mat.h"
#include "iostream"
#include "mex.h"
int main(){
float data[9] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
MATFile *pmat = matOpen("mattest.mat", "w");
const mwSize dims[] = { 3, 3 };
mxArray *pa1 = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxREAL);
mxArray *pa1T;
memcpy((void *)(mxGetPr(pa1)), (void *)data, 3 * 3 * sizeof(float));
mexCallMATLAB(0, NULL, 1, &pa1, "disp");
mexCallMATLAB(1, &pa1T, 1, &pa1, "transpose");
matPutVariable(pmat, "LocalDouble", pa1T);
mxDestroyArray(pa1);
mxDestroyArray(pa1T);
}
Even simple mexCallMATLAB(0, NULL, 1, &pa1, "disp"); causes this error. May be I didn't add something to my project? Even this sample code http://www.mathworks.com/help/matlab/apiref/rmvd_matlablink__4f95060e6fdb200c8b9414502f277cc0.html doesn't work(unresolved external symbol)
Update 2: Full output:
1>------ Build started: Project: wrtie_mat, Configuration: Debug x64 ------
1> Source.cpp
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup
1>C:\programs\misha\cuda\Projects\test projects\wrtie_mat\x64\Debug\wrtie_mat.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
When I tried to compile in matlab (mex Source.cpp), there was also error:
Unable to complete successfully.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!