Reputation: 325
I want to realize a function to translate the input variable of mxArray(from Matlab) to C++. For detail, if the input variable is a cell, it will be arranged to a vector.(Edited following advices) Like below,
#include "mex.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <omp.h>
#include <map>
#include <string>
using namespace std;
vector<vector<int>> transfer(const mxArray* ix)
{
mwSize Num = mxGetNumberOfElements(ix);
mxArray* ptr = NULL;
mxDouble* ptr_double;
vector<vector<int>> V(Num);
mwSize Num0;
int i,j;
for (i = 0; i < (int)Num; i++) {
ptr = mxGetCell(ix, i);
Num0 = mxGetNumberOfElements(ptr);
ptr_double = mxGetPr(ptr);
for (j = 0; j < (int)Num0; j++) {
V[i].push_back(*ptr_double++);
}
}
return V;
}
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
for (int i = 0; i < nrhs; i++) {
if (mxIsCell(prhs[i])) {
vector<vector<int>> V1 = transfer(prhs[i]);
for (int j = 0; j < V1.size(); j++)
{
for (int k = 0; k < V1[j].size(); k++) {
mexPrintf(" %d", V1[j][k]);
}
mexPrintf("\n size is %d\n", V1[j].size());
}
}
//else
}
return;
}
I now have a problem to name the above variable. I have to name the variable (like V1
) corresponding to the index i
(1
in the example). But I don't know how to realize it in c++. Could you tell me how to do it in C++?
I have searched a little on Internet and I found it seems impossible to have an dynamic name in C++. If this is true, Could anyone have some idea how to arrange these input variables, which type is vector> and double array? Perhaps struct? Then how to initial the struct dynamically is a bigger challenge to me.
Besides, I want to do this because I am using OpenMP which could not have any variable or pointer related to Matlab in the parallel loops. I must copy all the data from mx-
to C++ supported type data(like vector
).
Upvotes: 0
Views: 156
Reputation: 2636
Maybe something like this:
#include "mex.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <omp.h>
#include <map>
#include <string>
using namespace std;
vector<vector<double>> transfer(const mxArray* ix) // changed to double
{
mwSize Num = mxGetNumberOfElements(ix);
mxArray* ptr = NULL;
mxDouble* ptr_double;
vector<vector<double>> V(Num); // changed to double
mwSize Num0;
int i,j;
for (i = 0; i < (int)Num; i++) {
ptr = mxGetCell(ix, i);
if( ptr ) { // check for NULL pointer
if( !mxIsDouble(ptr) || mxIsComplex(ptr) || mxIsSparse(ptr) ) { // check for proper type etc.
mexErrMsgTxt("cell must contain full real double");
}
Num0 = mxGetNumberOfElements(ptr);
ptr_double = mxGetPr(ptr);
for (j = 0; j < (int)Num0; j++) {
V[i].push_back(*ptr_double++);
}
}
}
return V;
}
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
for (int i = 0; i < nrhs; i++) {
if (mxIsCell(prhs[i])) {
vector<vector<double>> V1 = transfer(prhs[i]); // changed to double
for (int j = 0; j < V1.size(); j++)
{
for (int k = 0; k < V1[j].size(); k++) {
mexPrintf(" %g", V1[j][k]); // changed to %g
}
mexPrintf("\n size is %d\n", V1[j].size());
}
}
//else
}
return;
}
Upvotes: 0
Reputation: 1202
Divide function.
BTW, new mxArray * [Num]
is bad idea. use std::vector<mxArray>
void transfer_impl(vector<vector<int>>& V, mxDouble*& ptr, mwSize Num0){
for (mwSize i = 0; j < Num0; j++) {
V[i].push_back(*ptr_double++);
}
}
Because the OpenMP is always crash
You should consider using C++17 Parallel Algorithms.
Upvotes: 1