フィルターのクリア

Use matrix c\c++ API with row-based indexing

1 回表示 (過去 30 日間)
Mikhail
Mikhail 2015 年 2 月 13 日
コメント済み: Mikhail 2015 年 2 月 17 日
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.

採用された回答

James Tursa
James Tursa 2015 年 2 月 14 日
You could transpose it after loading the mat file. Or you could call MATLAB to transpose it for you prior to saving the variable to a mat file. E.g.,
mxArray *pa1T;
mexCallMATLAB(1, &pa1T, 1, &pa1, "transpose");
matPutVariable(pmat, "LocalDouble", pa1T);
mxDestroyArray(pa1T);
mxDestroyArray(pa1);
  15 件のコメント
James Tursa
James Tursa 2015 年 2 月 17 日
編集済み: James Tursa 2015 年 2 月 17 日
I originally suggested mexCallMATLAB because you did not post your entire code. You only posted a code snippet that did not tell me you were building a C++ program rather than a mex routine. Once you posted code showing the int main(){, I knew you were compiling a program and not a mex routine, and have tried to steer you away from mexCallMATLAB ever since. Sorry for the confusion.
You cannot use ANY of the mex-only routines from the API in your program, and that includes mexCallMATLAB. So again I would go back to my advice a few posts earlier. (1) Open an Engine (not my choice), (2) manually code up the transpose yourself in C++ prior to saving it to the mat file (my choice), or (3) transpose it after reading the variable from the mat file.
Again, I apologize for originally suggesting mexCallMATLAB, but I did not know at the time you were compiling a program instead of a mex routine.
If you opt for (2) and need help let me know.
Mikhail
Mikhail 2015 年 2 月 17 日
Thanks again. I don't need further help, I think, I will transpose it in matlab (since it is only " ' " command).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite 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!

Translated by