Ich versuche, zwei Vektoren zu plotten, wo ich die Elemente von zwei mxArray
s von MATLAB (mit Visual C++ Compiler) gespeichert habe.Plot zwei Vektoren mit Mxarrays in C++ Compiler
test1
und test2
sind die mxArray
s aus der MAT-Datei, und wenn ich sie drucke, ist es in Ordnung. Aber wenn ich das Programm starte, ist die Figur leer.
#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);
}
dies unnötig kompliziert ist, es scheint, dass Sie die Last-Matrizen aus einer MAT-Datei als 'mx wollten Array' und plotten sie. Die gesamte Round-Trip-Konvertierung von/nach 'std :: vector' wird nicht benötigt! Verwenden Sie einfach direkt das von 'matGetVariable' zurückgegebene Array – Amro