📄 matlab_interface.h
字号:
#ifndef _matlab_interface_h_
#define _matlab_interface_h_
#include "engine.h"
#include "matrix.h"
#include "mex.h"
#include <conio.h>
#include <stdio.h>
#include <string.h>
class matlab_interface
{
protected:
Engine *ep;
public:
//default constructor
matlab_interface()
{
ep = NULL;
};
~matlab_interface()
{
if(ep!=NULL)
{
(*this).close_matlab();
}
};
void spawn_matlab(void)
{
if(ep == NULL)
{
ep = engOpen("\0");
//mexEvalString("stupid = 1");
engEvalString(ep,"jn_figure_handler init");
};
};
void close_matlab(void)
{
if(ep != NULL)
{
engClose(ep);
ep = NULL;
};
};
Engine * get_matlab_engine(void)
{
return ep;
};
void set_matlab_engine(Engine *ep1)
{
ep = ep1;
};
};
// uses jn_figure_handler.m
class matlab_figure_handler : public matlab_interface
{
private:
public:
// returns handler
double create_figure(void)
{
mxArray *temp_fig_h;
double return_hndl;
if(ep != NULL)
{
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSetName(temp_fig_h, "temp_fig_h");
*mxGetPr(temp_fig_h) = -1;
engEvalString(ep,"temp_fig_h = figure");
temp_fig_h = engGetArray(ep,"temp_fig_h");
return_hndl = (double)(*mxGetPr(temp_fig_h));
mxDestroyArray(temp_fig_h);
return return_hndl;
}
else
return -1;
};
double scatter_plot(double *x, double *y, int length)
{
mxArray *temp_fig_h;
double return_hndl;
mxArray *temp_line_x;
mxArray *temp_line_y;
if(ep != NULL)
{
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_x = mxCreateDoubleMatrix(1, length, mxREAL);
temp_line_y = mxCreateDoubleMatrix(1, length, mxREAL);
mxSetName(temp_fig_h, "temp_fig_h");
*mxGetPr(temp_fig_h) = -1;
mxSetName(temp_line_x, "temp_line_x");
mxSetName(temp_line_y, "temp_line_y");
memcpy((void *)mxGetPr(temp_line_x), (void *)x, length*sizeof(double));
memcpy((void *)mxGetPr(temp_line_y), (void *)y, length*sizeof(double));
engPutArray(ep,temp_fig_h);
engPutArray(ep,temp_line_x);
engPutArray(ep,temp_line_y);
engEvalString(ep,"temp_fig_h = scatter(temp_line_x,temp_line_y)");
temp_fig_h = engGetArray(ep,"temp_fig_h");
return_hndl = (double)(*mxGetPr(temp_fig_h));
mxDestroyArray(temp_fig_h);
mxDestroyArray(temp_line_x);
mxDestroyArray(temp_line_y);
return return_hndl;
}
else
return -1;
};
void set_figure_title(const char *c, double h_f)
{
mxArray *temp_string;
mxArray *temp_fig_h;
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSetName(temp_fig_h, "temp_fig_h");
*mxGetPr(temp_fig_h) = h_f;
engPutArray(ep,temp_fig_h);
temp_string = mxCreateString(c);
mxSetName(temp_string,"temp_string");
engPutArray(ep,temp_string);
engEvalString(ep,"figure(temp_fig_h)");
engEvalString(ep,"title(temp_string)");
mxDestroyArray(temp_fig_h);
mxDestroyArray(temp_string);
};
void set_xlabel(char *c, double h_f)
{
mxArray *temp_string;
mxArray *temp_fig_h;
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSetName(temp_fig_h, "temp_fig_h");
*mxGetPr(temp_fig_h) = h_f;
engPutArray(ep,temp_fig_h);
temp_string = mxCreateString(c);
mxSetName(temp_string,"temp_string");
engPutArray(ep,temp_string );
engEvalString(ep,"figure(temp_fig_h)");
engEvalString(ep,"xlabel(temp_string)");
mxDestroyArray(temp_string);
};
void set_ylabel(const char *c, double h_f)
{
mxArray *temp_string;
mxArray *temp_fig_h;
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSetName(temp_fig_h, "temp_fig_h");
*mxGetPr(temp_fig_h) = h_f;
engPutArray(ep,temp_fig_h);
temp_string = mxCreateString(c);
mxSetName(temp_string,"temp_string");
engPutArray(ep,temp_string );
engEvalString(ep,"figure(temp_fig_h)");
engEvalString(ep,"ylabel(temp_string)");
mxDestroyArray(temp_string);
};
void update_line(double *x, double *y, double h_f, double h_l, int length)
{
mxArray *temp_line_h;
mxArray *temp_line_x;
mxArray *temp_line_y;
if(ep != NULL)
{
temp_line_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_x = mxCreateDoubleMatrix(1, length, mxREAL);
temp_line_y = mxCreateDoubleMatrix(1, length, mxREAL);
mxSetName(temp_line_h, "temp_line_h");
mxSetName(temp_line_x, "temp_line_x");
mxSetName(temp_line_y, "temp_line_y");
*mxGetPr(temp_line_h) = h_l;
memcpy((void *)mxGetPr(temp_line_x), (void *)x, length*sizeof(double));
memcpy((void *)mxGetPr(temp_line_y), (void *)y, length*sizeof(double));
engPutArray(ep,temp_line_h);
engPutArray(ep,temp_line_x);
engPutArray(ep,temp_line_y);
engEvalString(ep,"jn_figure_handler('update_line',temp_line_h,temp_line_x,temp_line_y)");
mxDestroyArray(temp_line_h);
mxDestroyArray(temp_line_x);
mxDestroyArray(temp_line_y);
}
};
double add_line(double *x, double *y, double h_f, int length)
{
mxArray *temp_line_h;
mxArray *temp_line_x;
mxArray *temp_line_y;
mxArray *temp_fig_h;
double return_hndl;
if(ep != NULL)
{
// SET UP MATLAB MEMORY SPACE
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_x = mxCreateDoubleMatrix(1, length, mxREAL);
temp_line_y = mxCreateDoubleMatrix(1, length, mxREAL);
mxSetName(temp_line_h, "temp_fig_h");
mxSetName(temp_line_h, "temp_line_h");
mxSetName(temp_line_x, "temp_line_x");
mxSetName(temp_line_y, "temp_line_y");
memcpy((void *)mxGetPr(temp_fig_h), (void *)&h_f, sizeof(double));
memcpy((void *)mxGetPr(temp_line_x), (void *)x, length*sizeof(double));
memcpy((void *)mxGetPr(temp_line_y), (void *)y, length*sizeof(double));
engPutArray(ep,temp_fig_h);
engPutArray(ep,temp_line_x);
engPutArray(ep,temp_line_y);
// ISSUE MATLAB COMMANDS
engEvalString(ep,"figure(temp_fig_h)");
engEvalString(ep,"hold on");
engEvalString(ep,"temp_line_h = plot(temp_line_x,temp_line_y)");
engEvalString(ep,"hold off");
// RETRIEVE RELEVANT DATA
temp_line_h = engGetArray(ep,"temp_line_h");
return_hndl = (double)(*mxGetPr(temp_line_h));
// CLEAN UP MATLAB MEMEORY SPACE
mxDestroyArray(temp_fig_h);
mxDestroyArray(temp_line_h);
mxDestroyArray(temp_line_x);
mxDestroyArray(temp_line_y);
}
else
return_hndl = -1;
return return_hndl;
};
double add_line2(double *x, double *y, double h_f, int length)
{
mxArray *temp_line_h;
mxArray *temp_line_x;
mxArray *temp_line_y;
mxArray *temp_fig_h;
double return_hndl;
if(ep != NULL)
{
// SET UP MATLAB MEMORY SPACE
temp_fig_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_h = mxCreateDoubleMatrix(1, 1, mxREAL);
temp_line_x = mxCreateDoubleMatrix(1, length, mxREAL);
temp_line_y = mxCreateDoubleMatrix(1, length, mxREAL);
mxSetName(temp_line_h, "temp_fig_h");
mxSetName(temp_line_h, "temp_line_h");
mxSetName(temp_line_x, "temp_line_x");
mxSetName(temp_line_y, "temp_line_y");
memcpy((void *)mxGetPr(temp_fig_h), (void *)&h_f, sizeof(double));
memcpy((void *)mxGetPr(temp_line_x), (void *)x, length*sizeof(double));
memcpy((void *)mxGetPr(temp_line_y), (void *)y, length*sizeof(double));
engPutArray(ep,temp_fig_h);
engPutArray(ep,temp_line_x);
engPutArray(ep,temp_line_y);
// ISSUE MATLAB COMMANDS
engEvalString(ep,"figure(temp_fig_h)");
engEvalString(ep,"hold on");
engEvalString(ep,"temp_line_h = plot(temp_line_x,temp_line_y,'-o')");
engEvalString(ep,"hold off");
// RETRIEVE RELEVANT DATA
temp_line_h = engGetArray(ep,"temp_line_h");
return_hndl = (double)(*mxGetPr(temp_line_h));
// CLEAN UP MATLAB MEMEORY SPACE
mxDestroyArray(temp_fig_h);
mxDestroyArray(temp_line_h);
mxDestroyArray(temp_line_x);
mxDestroyArray(temp_line_y);
}
else
return_hndl = -1;
return return_hndl;
};
void delete_line(double h_l)
{
mxArray *temp_line_h;
// SET UP MATLAB MEMORY SPACE
temp_line_h = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSetName(temp_line_h, "temp_line_h");
*mxGetPr(temp_line_h) = h_l;
//ISSUE MATLAB COMMANDS
engEvalString(ep,"delete(temp_line_h)");
// CLEAN UP MATLAB MEMEORY SPACE
mxDestroyArray(temp_line_h);
};
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -