I'm trying to plot two vectors were i have stored the elements of two mxArray
s from MATLAB (using Visual C++ compiler).
test1
and test2
are the mxArray
s 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);
}