Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Mikhail
Mikhail am 13 Feb. 2015
Kommentiert: Mikhail am 17 Feb. 2015
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.

Akzeptierte Antwort

James Tursa
James Tursa am 14 Feb. 2015
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 Kommentare
James Tursa
James Tursa am 17 Feb. 2015
Bearbeitet: James Tursa am 17 Feb. 2015
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 am 17 Feb. 2015
Thanks again. I don't need further help, I think, I will transpose it in matlab (since it is only " ' " command).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by