0

I'm trying to plot two vectors were i have stored the elements of two mxArrays from MATLAB (using Visual C++ compiler).

test1 and test2 are the mxArrays from the MAT-file, and when I print them it is ok. But when I run the program the figure is blank.

#include "mat.h"
#include "NoneDynamic.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <engine.h>
using namespace std;
mxArray *T = NULL, *D = NULL;
Engine *ep;

void matread(const char *file, std::vector<double>& v)
{
    // open MAT-file
    MATFile *pmat = matOpen("DataTesting.mat", "r");
    if (pmat == NULL) return;
    // extract the specified variable
    mxArray *arr = matGetVariable(pmat, file);
    if (arr != NULL && mxIsDouble(arr) && !mxIsEmpty(arr)) {
        // copy data
        mwSize num = mxGetNumberOfElements(arr);
        double *pr = mxGetPr(arr);
        if (pr != NULL) {
            v.resize(num);
            v.assign(pr, pr + num);
        }
    }
    // cleanup
    mxDestroyArray(arr);
    matClose(pmat);
}
int main()
{
    SeenData object;
      vector<double> v;
    matread("Test1", v);
    vector <double> v1;
    matread("test2", v1);
    if (!(ep = engOpen(""))) {
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }
    T = mxCreateDoubleMatrix(1,100, mxREAL);
    memcpy((vector<double> *)mxGetPr(T), (vector<double>*) &v, sizeof(v));
    D = mxCreateDoubleMatrix(1,100, mxREAL);
    memcpy((vector<double> * )mxGetPr(D), (vector<double>*) &v1, sizeof(v1));
    engPutVariable(ep, "T", T);
    engPutVariable(ep, "D", D);
    engEvalString(ep, "plot (T,D)");
    fgetc(stdin);// for pausing long enough to see the plot
    engEvalString(ep, "close;");
    mxDestroyArray(T);
    mxDestroyArray(D);
    engClose(ep);

}
Amro
  • 123,847
  • 25
  • 243
  • 454
  • this is unnecessarily complicated, it seems you wanted the load matrices from a MAT-file as `mxArray` and plot them. The whole round-trip conversion to/from `std::vector` is not needed! Just directly use the array returned by `matGetVariable` – Amro May 23 '16 at 12:26

1 Answers1

0

With the following instructions

memcpy((vector<double> *)mxGetPr(T), (vector<double>*) &v, sizeof(v));

memcpy((vector<double> * )mxGetPr(D), (vector<double>*) &v1, sizeof(v1));

I suppose your intention was copy in mxGetPr(T) and mxGetPr(D) the data contained in v and v1.

But

1) std::vector<double> is a class with (I suppose) a pointer in it; so sizeof(v) and sizeof(v1) isn't the size of the contained data but a fixed size (24 for my clang++)

2) mem copying the pointer of v (and v1) is mem copying the struct vector, not the data contained in it.

3) std::memcpy() receive void pointers; so your cast is unuseful

If you really want use std::memcpy(), and you are using a C++11/C++14 compiler, I suppose you can write

memcpy(mxGetPr(T), v.data(), sizeof(double)*v.size());
memcpy(mxGetPr(D), v1.data(), sizeof(double)*v1.size());

otherwise, the good old

double * tp = mxGetPr(T);

for ( std::size_t ui = 0 ; ui < v.size() ; ++ui )
   tp[ui] = v[ui];


double * td = mxGetPr(D);

for ( std::size_t ui = 0 ; ui < v1.size() ; ++ui )
   td[ui] = v1[ui];

p.s.: sorry for my bad English

max66
  • 65,235
  • 10
  • 71
  • 111