Example #1
0
void MATLABPLOTTER::plotData(std::vector <double> &timeVector, std::vector <double> &signalVector, bool new_figure)
{
	
	mxArray *data_time = NULL;
	mxArray *data_Signal = NULL;
		//Scan laser


		double *time_data_ptr = (double*) calloc(timeVector.size(), sizeof(double));
		if (time_data_ptr==NULL) exit(1);
		double *Signal_data_ptr = (double*) calloc(signalVector.size(), sizeof(double));
		if (Signal_data_ptr==NULL) exit(1);

	for(unsigned int i=0;i < signalVector.size(); i++) {
		double stra = timeVector.at(i);
		
		double strb = signalVector.at(i);
		time_data_ptr[i] = stra;
		Signal_data_ptr[i] = strb;
		}


	data_time = mxCreateDoubleMatrix(1, timeVector.size(), mxREAL);
	memcpy((char*) mxGetPr(data_time), time_data_ptr, timeVector.size()*sizeof(double));

	data_Signal = mxCreateDoubleMatrix(1, timeVector.size(), mxREAL);
	memcpy((char*) mxGetPr(data_Signal), Signal_data_ptr, timeVector.size()*sizeof(double));

	
	 // Place the variable dataTime & dataSignal into the MATLAB workspace
	 

	engPutVariable(ep, "dataTime", data_time);
	engPutVariable(ep, "dataSignal", data_Signal);

	// Plot the result
	 
	
	if (new_figure) {
		engEvalString(ep, "close;");
		engEvalString(ep, "figure;");
	}
	else {
	engEvalString(ep, "hold on;");
	}
	
	engEvalString(ep, "plot(dataTime(:)',dataSignal(:)', color_codes(round(rand*4+1)) );");
	engEvalString(ep, "title('Scope Trace');");
	engEvalString(ep, "xlabel('Time (s)');");
	engEvalString(ep, "ylabel('Volts');");
	engEvalString(ep, "iter=iter+1;");

	free(Signal_data_ptr);
	free(time_data_ptr);

}
/* Record data for MATLAB processing. */
void recordData() {
	if (dataCounter<numberOfSamples) {
		dataSetVisual[dataCounter] = shift+((sizeOrig*size)/2);
		print_time();
	} else if (dataCounter == numberOfSamples) {
		engPutVariable(ep, "cppVisualSamples", cppVisualSamples); 
		engPutVariable(ep, "cppTimingSamples", cppTimingSamples);
		engEvalString(ep, "save visualdata.mat");
		fprintf(stderr, "Recording of %i samples is complete.  Data saved as \"visualdata.mat\".\n", numberOfSamples);
		recordMode = 0;
		dataCounter++;
	}
}
Example #3
0
void MATLABPLOTTER::plotlockpoints(std::vector <double> &FITFREQ_vector, std::vector <double> &FITDAQ_vector)
{
	
	mxArray *fit_data_freq = NULL;
	mxArray *fit_data_DAQ = NULL;
		//Scan laser


		double *fit_freq_data_ptr = (double*) calloc(FITFREQ_vector.size(), sizeof(double));
		if (fit_freq_data_ptr==NULL) exit(1);
		double *fit_DAQ_data_ptr = (double*) calloc(FITDAQ_vector.size(), sizeof(double));
		if (fit_DAQ_data_ptr==NULL) exit(1);

	for(unsigned int i=0;i < FITDAQ_vector.size(); i++) {
		double stra = FITFREQ_vector.at(i);
		
		double strb = FITDAQ_vector.at(i);
		fit_freq_data_ptr[i] = stra;
		fit_DAQ_data_ptr[i] = strb;
		}


	fit_data_freq = mxCreateDoubleMatrix(1, FITFREQ_vector.size(), mxREAL);
	memcpy((char*) mxGetPr(fit_data_freq), fit_freq_data_ptr, FITFREQ_vector.size()*sizeof(double));

	fit_data_DAQ = mxCreateDoubleMatrix(1, FITFREQ_vector.size(), mxREAL);
	memcpy((char*) mxGetPr(fit_data_DAQ), fit_DAQ_data_ptr, FITFREQ_vector.size()*sizeof(double));

	/*
	 * Place the variable dataFreq & dataDAQ into the MATLAB workspace
	 */

	engPutVariable(ep, "fitdataFreq", fit_data_freq);
	engPutVariable(ep, "fitdataDAQ", fit_data_DAQ);

	/* Plot the result
	 */
	engEvalString(ep, "i=1");
	for(unsigned int j=1; j <= FITFREQ_vector.size(); j++) {
		engEvalString(ep, "hold on;");
		engEvalString(ep, "plot(fitdataFreq(i)',fitdataDAQ(i)', 's','MarkerSize',10, 'MarkerFaceColor','g','MarkerEdgeColor', 'r');");
		engEvalString(ep, "i=i+1;");
	}

	free(fit_DAQ_data_ptr);
	free(fit_freq_data_ptr);

}
Example #4
0
void MATLABPLOTTER::plotfreqscan(std::vector <double> &FREQ_vector, std::vector <double> &DAQ_vector)
{
	
	mxArray *data_freq = NULL;
	mxArray *data_DAQ = NULL;
		//Scan laser


		double *freq_data_ptr = (double*) calloc(FREQ_vector.size(), sizeof(double));
		if (freq_data_ptr==NULL) exit(1);
		double *DAQ_data_ptr = (double*) calloc(DAQ_vector.size(), sizeof(double));
		if (DAQ_data_ptr==NULL) exit(1);

	for(unsigned int i=0;i < DAQ_vector.size(); i++) {
		double stra = FREQ_vector.at(i);
		
		double strb = DAQ_vector.at(i);
		freq_data_ptr[i] = stra;
		DAQ_data_ptr[i] = strb;
		}


	data_freq = mxCreateDoubleMatrix(1, FREQ_vector.size(), mxREAL);
	memcpy((char*) mxGetPr(data_freq), freq_data_ptr, FREQ_vector.size()*sizeof(double));

	data_DAQ = mxCreateDoubleMatrix(1, FREQ_vector.size(), mxREAL);
	memcpy((char*) mxGetPr(data_DAQ), DAQ_data_ptr, FREQ_vector.size()*sizeof(double));

	/*
	 * Place the variable dataFreq & dataDAQ into the MATLAB workspace
	 */

	engPutVariable(ep, "dataFreq", data_freq);
	engPutVariable(ep, "dataDAQ", data_DAQ);

	/* Plot the result
	 */
	//engEvalString(ep, "hold on;");
	engEvalString(ep, "figure(1);");
	engEvalString(ep, "plot(dataFreq(:)',dataDAQ(:)');");
	engEvalString(ep, "title('Rb Scan');");
	engEvalString(ep, "xlabel('Frequency (GHz)');");
	engEvalString(ep, "ylabel('Absorbtion (V)');");

	free(DAQ_data_ptr);
	free(freq_data_ptr);

}
void SignalProcessTools::put2DData(double *data, int n1, int n2, string name)
{
	mxArray *W;
	W = mxCreateDoubleMatrix(n1, n2, mxREAL);
	memcpy((void *)mxGetPr(W), (void *)(data), n1*n2*sizeof(double));
	engPutVariable(ep, name.c_str(), W);
}
Example #6
0
int PASCAL WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpszCmdLine,
                    int       nCmdShow){
	char buffer[BUFSIZE];
	mxArray *app;
	Engine *ep;

	/* 啟動 MATLAB 引擎 */
	if (!(ep = engOpen(NULL))){		// 產生一個 MATLAB 引擎物件
		MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",
			(LPSTR)"plotViaMatlab01.c", MB_OK);
		exit(-1);
	}

	/* 切換目錄並執行 plotSine.m */
	_getcwd(buffer, BUFSIZE);		// 將此程式所在目錄存入字串 buffer
	app = mxCreateString(buffer);		// 產生 MATLAB 內部的字串變數 app
	engPutVariable(ep, "appDir", app);	// 將字串變數 app 至入工作空間的變數 appDir
	engEvalString(ep, "cd(appDir)");	// 將 MATLAB 的工作目錄切換至字串 appDir 所指定的目錄
	engEvalString(ep, "plotSine");		// 執行同目錄下的 plotSine.m
	
	/* 取得 MATLAB 輸出訊息 */
	engOutputBuffer(ep, buffer, BUFSIZE);	// 設定 buffer 可以接收 MATLAB 的輸出訊息
	engEvalString(ep, "whos");		// 在 MATLAB 引擎執行 whos 指令
	MessageBox((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);		// 顯示 buffer 的內容

	engClose(ep);				// 最後關閉 MATLAB 引擎
	return(0);
}
Example #7
0
static int
p_create_cell_matrix_and_copy1(void)
{
  int rows, cols;
  mxArray *mat;
  YAP_Term tl = YAP_ARG3;

  rows = YAP_IntOfTerm(YAP_ARG1);
  cols = YAP_IntOfTerm(YAP_ARG2);
  if (!(mat = mxCreateCellMatrix(rows, cols)))
    return FALSE;
    while (YAP_IsPairTerm(tl)) {
    YAP_Term th = YAP_HeadOfTerm(tl);
    int off = MAT_ACCESS(YAP_IntOfTerm(YAP_ArgOfTerm(1,th))-1,
			 YAP_IntOfTerm(YAP_ArgOfTerm(2,th))-1,
			 rows,cols);
    mxArray *mat2 = get_array(YAP_ArgOfTerm(3,th));
    mxSetCell(mat,off, mat2);
    tl = YAP_TailOfTerm(tl);
  }
  if (YAP_IsAtomTerm(YAP_ARG4)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
  }
  return YAP_Unify(YAP_ARG4,address2term(mat));
}
Example #8
0
int main(int argc, const char *argv[])

{
  Engine *ep;
  char buff[10240];
  int i;

  /* matlab must be in the PATH! */
  if (!(ep = engOpen("matlab -nodisplay"))) {
    fprintf(stderr, "Can't start MATLAB engine\n");
    return -1;
  }
  engOutputBuffer(ep, buff, 10239);

  /* load the mex file */
  if(argc<2){
    fprintf(stderr, "Error. Give full path to the MEX file as input parameter.\n");
    return -1;
  }
  void *handle = dlopen(argv[1], RTLD_NOW);
  if(!handle){
    fprintf(stderr, "Error loading MEX file: %s\n", strerror(errno));
    return -1;
  }

  /* grab mexFunction handle */
  mexFunction_t mexfunction = (mexFunction_t)dlsym(handle, "mexFunction");
  if(!mexfunction){
    fprintf(stderr, "MEX file does not contain mexFunction\n");
    return -1;
  }

  /* load input data - for convenience do that using MATLAB engine */
  /* NOTE: parameters are MEX-file specific, so one has to modify this*/
  /* to fit particular needs */
  engEvalString(ep, "load input.mat");
  mxArray *arg1 = engGetVariable(ep, "im1");
  mxArray *arg2 = engGetVariable(ep, "im2");
  mxArray *arg3 = engGetVariable(ep, "szx");
  mxArray *arg4 = engGetVariable(ep, "szy");
  mxArray *arg5 = engGetVariable(ep, "ngh");
  mxArray *pargout[1] = {0};
  const mxArray *pargin[5] = {arg1, arg2,arg3, arg4,arg5};

  /* execute the mex function */
  mexfunction(1, pargout, 5, pargin);

  /* print the results using MATLAB engine */
  engPutVariable(ep, "result", pargout[0]);
  engEvalString(ep, "result");
  printf("%s\n", buff);

  /* cleanup */
  mxDestroyArray(pargout[0]);
  engEvalString(ep, "clear all;");
  dlclose(handle);
  engClose(ep);

  return 0;
}
Example #9
0
void Search_Synonym_LSI() //这里主要是把相关度前二十的诗词打包成向量投射到词汇空间

{
	double rvpoems[POEMSNUM];
	memset(rvpoems , 0 , sizeof(rvpoems));
	for (int i = 0 ; i <= 19 ; ++i)
	{
		rvpoems[ vresult[i] -1] = 1;
	}
	mxArray * vpoems_m = mxCreateDoubleMatrix(1,POEMSNUM, mxREAL);  
	memcpy(mxGetPr(vpoems_m), rvpoems, POEMSNUM * sizeof(double));  
	engPutVariable(ep, "R",vpoems_m);
	engEvalString(ep, "BS=zeros(7474,1);");
	engEvalString(ep, "AS = R*(V)*(inv(U));");
	engEvalString(ep, "k = 1;");
	for (int i=1 ;i<= WORDNUM ; ++i)
	{
		engEvalString(ep, "BS(k,1) = dot(AS,S(k,:)) / (norm(AS)*norm(S(k,:)) );");
		engEvalString(ep, "k = k + 1;");
	}
	engEvalString(ep, "[LS,INDS]=sort(BS,'descend');" );	
	mxArray * vsyn_m = engGetVariable(ep, "INDS" );
	mxArray * srelation_m = engGetVariable(ep , "LS");
	memcpy(rvsyn, mxGetPr(vsyn_m) , WORDNUM * sizeof(double));
	memcpy(srelation , mxGetPr(srelation_m) , WORDNUM * sizeof(double));
	mxDestroyArray(vpoems_m);
	mxDestroyArray(vsyn_m);
}
void SignalProcessTools::putVector(vector data, string name)
{
	mxArray *W;
	W = mxCreateDoubleMatrix(1, data.length, mxREAL);
	memcpy((void *)mxGetPr(W), (void *)(data.data), data.length*sizeof(double));
	engPutVariable(ep, name.c_str(), W);
}
void SignalProcessTools::putWaveData(WaveData data)
{
	putVector(data.wave, "W");
	mxArray *Fs;
	Fs = mxCreateDoubleScalar(data.sampleRate);
	engPutVariable(ep, "Fs", Fs);
}
Example #12
0
EXPORT int create_matlab(OBJECT **obj, OBJECT *parent) 
{
	try 
	{
		*obj = gl_create_object(oclass,sizeof(mxArray*));
		if (*obj!=NULL)
		{
			gl_set_parent(*obj,parent);
			char createcall[1024];

			if (engPutVariable(engine,"object",defaults))
			{
				gl_error("matlab::%s_create(...) unable to set defaults for object", oclass->name);
				throw "create failed";
			}
			if (parent)
			{
				// @todo transfer parent data in matlab create call
				mxArray *pParent = mxCreateStructMatrix(0,0,0,NULL);
				engPutVariable(engine,"parent",pParent);
				sprintf(createcall,"create(object,parent)");
			}
			else
				sprintf(createcall,"create(object,[])");
			if (engEvalString(engine,createcall)!=0)
			{
				gl_error("matlab::%s_create(...) unable to evaluate '%s' in Matlab", oclass->name, createcall);
				throw "create failed";
			}
			else
				gl_matlab_output();
			mxArray *ans= engGetVariable(engine,"ans");
			mxArray **my = OBJECTDATA(*obj,mxArray*);
			if (ans && mxIsClass(ans,oclass->name))
				*my = engGetVariable(engine,"ans");
			else
			{
				*my = NULL;
				gl_error("matlab::@%s/create(...) failed to return an object of class %s",oclass->name,oclass->name);
				throw "create failed";
			}
			return 1;
		}
		else
			throw "create failed due to memory allocation failure";
	} 
Example #13
0
static int
cp_back(YAP_Term vart, mxArray *mat)
{
  if (!YAP_IsAtomTerm(vart)) {
    return TRUE;
  }
  /* save back to matlab */
  return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(vart)), mat);
}
Example #14
0
int main(){
	Engine *ep;
	mxArray *mX = NULL, *mY = NULL;
	const size_t N = 1024;
	
	if (!(ep = engOpen(NULL))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return EXIT_FAILURE;
	}

	mX = mxCreateDoubleMatrix(1, N, mxREAL);
	mY = mxCreateDoubleMatrix(1, N, mxREAL);

	double *x = (double *)mxGetPr(mX);
	double *y = (double *)mxGetPr(mY);
	for (size_t i=0; i<N; ++i){
		x[i] = 2.0*M_PI*i/N;
		y[i] = sin(x[i]);
	}
	
	engPutVariable(ep, "x", mX);
	engPutVariable(ep, "y", mY);

	engEvalString(ep, "h = figure;");
	engEvalString(ep, "plot(x, y); hold on;");
	engEvalString(ep, "plot([0 0],ylim,'k'); plot(xlim,[0 0],'k');");
	engEvalString(ep, "title('y = sin(x)');");
	engEvalString(ep, "xlabel('x');");
	engEvalString(ep, "ylabel('y');");
	engEvalString(ep, "grid on; grid minor; box off;");
	engEvalString(ep, "print(h,'-dpng','sin.png');");
	engEvalString(ep, "close all;");
	
	mxDestroyArray(mX);
	mxDestroyArray(mY);

	printf("Ok. Press ENTER to exit\n");
	getchar();

	engClose(ep);
	
	return EXIT_SUCCESS;
}
Example #15
0
PETSC_EXTERN PetscErrorCode MatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
{
  PetscErrorCode ierr;
  mxArray        *mat;

  PetscFunctionBegin;
  mat  = MatSeqAIJToMatlab((Mat)obj);if (!mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot create MATLAB matrix");
  ierr = PetscObjectName(obj);CHKERRQ(ierr);
  engPutVariable((Engine*)mengine,obj->name,mat);
  PetscFunctionReturn(0);
}
Example #16
0
int object_to_string(void *addr, char *value, int size)
{
	mxArray **my = (mxArray**)addr;
	if (*my!=NULL)
	{
		engPutVariable(engine,"object",*my);
		engEvalString(engine,"disp(char(object));");
		output[strlen(output)-1]='\0'; // trim CR off end
		return _snprintf(value,size-1,"[[%s]]",output);
	}
	else
		return 0;
}
Example #17
0
//put a complex array
void engputc(const char* VarName,
		     const int* mmaDim, int Depth,
		     const double* Re, int ReLen,
		     const double* Im, int ImLen)
{
	mxArray* MxVar = NULL;		//the variable to be put
	bool SUCCESS = true;		//success flag
	int i;						// generic iterator

	mwSize matlabDim[Depth];	// dimensions array in MATLAB format

	for (i=0; i < Depth; ++i)
		matlabDim[i] = (mwSize) mmaDim[i];


	if (NULL == Eng)	//if not opened yet, open it
	{
		msg("eng::noMLB");	//message 
		SUCCESS = false;
		goto epilog;
	}
	
	//create mxArray 
	MxVar = mxCreateNumericArray(Depth, matlabDim, mxDOUBLE_CLASS, mxCOMPLEX);
	if (NULL == MxVar)
	{
		msg("engPut::ercrt");
		SUCCESS = false;
		goto epilog;
	}
	//and populate
	memcpy((void *)(mxGetPr(MxVar)), (void *)Re, ReLen * sizeof(double));
	memcpy((void *)(mxGetPi(MxVar)), (void *)Im, ImLen * sizeof(double));

	//put
	if(engPutVariable(Eng, VarName, MxVar))	//not successful
	{
		msg("engPut::erput");
		SUCCESS = false;
		goto epilog;
	}
	
epilog:
	if (MxVar != NULL)
		mxDestroyArray(MxVar);

	if(SUCCESS)
		MLPutString(stdlink, VarName);
	else
		MLPutSymbol(stdlink, "$Failed");
}
Example #18
0
void CEvaluateResultDlg::DrawFactor(const Factor &factor, const FactorResult &factorResult)
{
    mwSize size = factor.AlgorithmOutputEnd - factor.AlgorithmOutputStart + 1;
    Array *t = mxCreateDoubleMatrix(1, size, mxREAL);

    double *p = mxGetPr(t);
    for (UINT32 i = factor.AlgorithmOutputStart; i <= factor.AlgorithmOutputEnd; ++i)
    {
        p[i - factor.AlgorithmOutputStart] = i;
    }

    engPutVariable(m_Engine, "t", t);
    engPutVariable(m_Engine, "y", factorResult.Result);

    engEvalString(m_Engine, "plot(t, y);");
    engEvalString(m_Engine, "xlabel('t');");

    string cmd = "ylabel('" + Utility::Wstring2String(factor.Name) + "');";
    engEvalString(m_Engine, cmd.c_str());

    mxDestroyArray(t);
    // mxDestroyArray(factorResult.Result);
}
Example #19
0
static int
p_create_double_vector(void)
{
  mwSize dims[1];
  mxArray *mat;

  dims[0] = YAP_IntOfTerm(YAP_ARG1);
  if (!(mat = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL)))
    return FALSE;
  if (YAP_IsAtomTerm(YAP_ARG2)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), mat);
  }
  return YAP_Unify(YAP_ARG2,address2term(mat));
}
Example #20
0
static int
p_create_cell_vector(void)
{
  mwSize dims[1];
  mxArray *mat;

  dims[0] = YAP_IntOfTerm(YAP_ARG1);
  if (!(mat = mxCreateCellArray(1, dims)))
    return FALSE;
  if (YAP_IsAtomTerm(YAP_ARG2)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), mat);
  }
  return YAP_Unify(YAP_ARG2,address2term(mat));
}
Example #21
0
static int
p_create_double_array(void)
{
  int rows, cols;
  mxArray *mat;

  rows = YAP_IntOfTerm(YAP_ARG1);
  cols = YAP_IntOfTerm(YAP_ARG2);
  if (!(mat = mxCreateDoubleMatrix(rows, cols, mxREAL)))
    return FALSE;
  if (YAP_IsAtomTerm(YAP_ARG3)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
  }
  return YAP_Unify(YAP_ARG3,address2term(mat));
}
Example #22
0
// Send a matrix to MATLAB
IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);
  
  mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  double *pM = mxGetPr(A);
  
  int c = 0;
  for(int j=0; j<M.cols();++j)
    for(int i=0; i<M.rows();++i)
      pM[c++] = double(M(i,j))+1;
  
  engPutVariable(*mlengine, name.c_str(), A);
  mxDestroyArray(A);
}
Example #23
0
/*@C
    PetscMatlabEnginePutArray - Puts an array into the MATLAB space, treating it as a Fortran style (column major ordering) array. For parallel objects,
      each processors part is put in a separate  MATLAB process.

    Collective on PetscObject

    Input Parameters:
+    mengine - the MATLAB engine
.    m,n - the dimensions of the array
.    array - the array (represented in one dimension)
-    name - the name of the array

   Level: advanced

.seealso: PetscMatlabEngineDestroy(), PetscMatlabEngineCreate(), PetscMatlabEngineGet(),
          PetscMatlabEngineEvaluate(), PetscMatlabEngineGetOutput(), PetscMatlabEnginePrintOutput(),
          PETSC_MATLAB_ENGINE_(), PetscMatlabEnginePut(), MatlabEngineGetArray(), PetscMatlabEngine
@*/
PetscErrorCode  PetscMatlabEnginePutArray(PetscMatlabEngine mengine,int m,int n,const PetscScalar *array,const char name[])
{
  PetscErrorCode ierr;
  mxArray        *mat;

  PetscFunctionBegin;
  ierr = PetscInfo1(0,"Putting MATLAB array %s\n",name);CHKERRQ(ierr);
#if !defined(PETSC_USE_COMPLEX)
  mat = mxCreateDoubleMatrix(m,n,mxREAL);
#else
  mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
#endif
  ierr = PetscMemcpy(mxGetPr(mat),array,m*n*sizeof(PetscScalar));CHKERRQ(ierr);
  engPutVariable(mengine->ep,name,mat);

  ierr = PetscInfo1(0,"Put MATLAB array %s\n",name);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Example #24
0
static int
p_call_matlab(void)
{
  YAP_Term tlength = YAP_ARG2,
    tl = YAP_ARG3,
    tname = YAP_ARG1,
    tolength = YAP_ARG4,
    tout = YAP_ARG5;
  int i = 0;
  mxArray *inps[50], *outs[50];
  const char *name;
  int olength = YAP_IntOfTerm(tolength);

  if (!YAP_IsAtomTerm(tname))
    return FALSE;
  name = YAP_AtomName(YAP_AtomOfTerm(tname));
  if (!YAP_IsIntTerm(tlength))
    return FALSE;
  while (YAP_IsPairTerm(tl)) {
    inps[i] = get_array(YAP_HeadOfTerm(tl));
    i++;
    tl = YAP_TailOfTerm(tl);
  }
  if (mexCallMATLAB(olength, outs, i, inps, name))
    return FALSE;
  /* output arguments */
  if (YAP_IsPairTerm(tout)) {
    for (i=0; i<olength; i++) {
      YAP_Term ti = YAP_HeadOfTerm(tout);
      if (YAP_IsAtomTerm(ti)) {
	return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(ti)), outs[i]);
      } else {
	return YAP_Unify(ti,address2term(outs[i]));
      }
    }    
  } else {
    YAP_Term to = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
    for (i=olength; i>0; i--) {
      to = YAP_MkPairTerm(address2term(outs[i-1]),to);
    }    
  }
  return TRUE;
}
Example #25
0
void Search_LSI()
{
	mxArray * vquery_m = mxCreateDoubleMatrix(1,WORDNUM, mxREAL);  
	memcpy(mxGetPr(vquery_m), vquery, WORDNUM * sizeof(double));  
	engPutVariable(ep, "Q",vquery_m);
	engEvalString(ep, "B=zeros(472,1);");
	engEvalString(ep, "A = Q*(S)*(inv(U));");
	engEvalString(ep, "k = 1;");
	for (int i=1 ;i<= WORDNUM ; ++i)
	{
		engEvalString(ep, "B(k,1) = dot(A,V(k,:)) / (norm(A)*norm(V(k,:)) ) ;");
		engEvalString(ep, "k = k + 1;");
	}
	engEvalString(ep, "[L,IND]=sort(B,'descend');" );	
    mxArray * vresult_m = engGetVariable(ep, "IND" );
	mxArray * vrelation_m = engGetVariable(ep, "L") ;
	memcpy(rvresult, mxGetPr(vresult_m) , POEMSNUM * sizeof(double));
	memcpy(vrelation, mxGetPr(vrelation_m) , POEMSNUM * sizeof(double));
	mxDestroyArray(vquery_m);
}
Example #26
0
void Uav::useMatlabEngine()
{
  Q_ASSERT(matlabEngine);
  
  /* 
   * Create a variable for our data
   */
  mxArray* pos2d = mxCreateDoubleMatrix(1, 2, mxREAL);
  
  
  //NOTE: the UAV's (x,y,z) coordinates are given wrt. to the 
  // North/x-East/y-Down/y frame of reference.
  // Matlab's plot function assume the standart mathematical 
  // X/East-Y/North-Z/Up frame of reference...
  
  memcpy(static_cast<void*>(mxGetPr(pos2d)),
         static_cast<const void*>( &(ownship().position2D().y()) ),
         sizeof(ownship().position2D().x())) ;
         
  memcpy(static_cast<void*>(mxGetPr(pos2d)+1 ),
         static_cast<const void*>( &(ownship().position2D().x()) ),
         sizeof(ownship().position2D().y())) ;         
        
  /*
   * Place the variable "pos2d" into the MATLAB workspace
   */
  engPutVariable(matlabEngine, "pos2d", pos2d);
  
  // append the current position to the track
  engEvalString(matlabEngine, "ownshipTrack = [ownshipTrack; pos2d];");
  
  /*
   * Plot the result
   */
  engEvalString(matlabEngine, "plot(ownshipTrack(:,1),ownshipTrack(:,2),'-b',pos2d(1),pos2d(2),'+r');");
  
  /*
   * Clean up
   */
  mxDestroyArray(pos2d);
}
Example #27
0
static int
p_set_float_array(void)
{
  int rows, cols, i = 0, j = 0;
  double *input;
  mxArray *mat;
  YAP_Term tl = YAP_ARG3;

  rows = YAP_IntOfTerm(YAP_ARG1);
  cols = YAP_IntOfTerm(YAP_ARG2);
  if (!(mat = mxCreateDoubleMatrix(rows, cols, mxREAL)))
    return FALSE;
  input = mxGetPr(mat);
  /* copy ints to matrix. */
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      YAP_Term th;

      if (!YAP_IsPairTerm(tl)) {
	return FALSE;
      }
      th = YAP_HeadOfTerm(tl);
      if (YAP_IsIntTerm(th)) {
	input[MAT_ACCESS(i,j,rows,cols)] = YAP_IntOfTerm(th);
      } else if (YAP_IsFloatTerm(th)) {
	input[MAT_ACCESS(i,j,rows,cols)] = YAP_FloatOfTerm(th);
      } else {
	/* ERROR */
	return FALSE;
      }
      tl = YAP_TailOfTerm(tl);
    }
  }
  if (YAP_IsAtomTerm(YAP_ARG4)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
  }
  return YAP_Unify(YAP_ARG4,address2term(mat));
}
Example #28
0
PetscErrorCode  VecMatlabEnginePut_Default(PetscObject obj,void *mengine)
{
  PetscErrorCode ierr;
  PetscInt       n;
  Vec            vec = (Vec)obj;
  PetscScalar    *array;
  mxArray        *mat;

  PetscFunctionBegin;
  ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
  ierr = VecGetLocalSize(vec,&n);CHKERRQ(ierr);
#if !defined(PETSC_USE_COMPLEX)
  mat  = mxCreateDoubleMatrix(n,1,mxREAL);
#else
  mat  = mxCreateDoubleMatrix(n,1,mxCOMPLEX);
#endif
  ierr = PetscMemcpy(mxGetPr(mat),array,n*sizeof(PetscScalar));CHKERRQ(ierr);
  ierr = PetscObjectName(obj);CHKERRQ(ierr);
  engPutVariable((Engine*)mengine,obj->name,mat);

  ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Example #29
0
void MATLABPLOTTER::plotVector(std::vector <double> &inputVector)
{
	
	mxArray *data_input = NULL;

		double *input_data_ptr = (double*) calloc(inputVector.size(), sizeof(double));
		if (input_data_ptr==NULL) exit(1);


	for(unsigned int i=0;i < inputVector.size(); i++) {
		double stra = inputVector.at(i);
		
		input_data_ptr[i] = stra;
		}


	data_input = mxCreateDoubleMatrix(1, inputVector.size(), mxREAL);
	memcpy((char*) mxGetPr(data_input), input_data_ptr, inputVector.size()*sizeof(double));

	/*
	 * Place the variable data_input into the MATLAB workspace
	 */

	engEvalString(ep, "clear inputData;");
	engPutVariable(ep, "inputData", data_input);

	/* Plot the result
	 */
	
	engEvalString(ep, "close all;");
	engEvalString(ep, "figure(1);");
	engEvalString(ep, "plot(inputData);");


	free(input_data_ptr);

}
Example #30
0
static int
p_set_float_vector(void)
{
  mwSize len[1];
  int i = 0;
  double *input;
  mxArray *mat;
  YAP_Term tl = YAP_ARG2;

  len[0] = YAP_IntOfTerm(YAP_ARG1);
  if (!(mat = mxCreateNumericArray(1,len, mxDOUBLE_CLASS, mxREAL)))
    return FALSE;
  input = mxGetPr(mat);
  /* copy ints to matrix. */
  for (i = 0; i < len[0]; i++) {
    YAP_Term th;

    if (!YAP_IsPairTerm(tl)) {
      return FALSE;
    }
    th = YAP_HeadOfTerm(tl);
    if (YAP_IsIntTerm(th)) {
      input[i] = YAP_IntOfTerm(th);
    } else if (YAP_IsFloatTerm(th)) {
      input[i] = YAP_FloatOfTerm(th);
    } else {
      /* ERROR */
      return FALSE;
    }
    tl = YAP_TailOfTerm(tl);
  }
  if (YAP_IsAtomTerm(YAP_ARG3)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
  }
  return YAP_Unify(YAP_ARG3,address2term(mat));
}