Thread Subject: Matlab C++ Interface

Subject: Matlab C++ Interface

From: sadel

Date: 8 Nov, 2009 18:57:01

Message: 1 of 2

Hi all!
I use this version of Microsoft Visual C++ Express Edition
Microsoft Visual Studio 2005
Version 8.0.50727.867
Microsoft .NET Framework
Version 2.0.50727 SP2
Installed Edition: VC Express
I use this version of Matlab:
MATLAB Version 7.8.0.347 (R2009a)
and i use this operating System: Microsoft Windows Vista Version 6.0 (Build 6001: Service Pack 1) Home premium

 I am trying to compile 'engwindemo.cpp' example but i take this error message:

1>Compiling...
1>win.cpp
1>.\win.cpp(39) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>.\win.cpp(104) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>.\win.cpp(113) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>.\win.cpp(122) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Can anyone help me?

Thank you in advance!

Subject: Matlab C++ Interface

From: sadel

Date: 9 Nov, 2009 10:30:04

Message: 2 of 2

"sadel " <sadel2006@yahoo.gr> wrote in message <hd749t$fqk$1@fred.mathworks.com>...
> Hi all!
> I use this version of Microsoft Visual C++ Express Edition
> Microsoft Visual Studio 2005
> Version 8.0.50727.867
> Microsoft .NET Framework
> Version 2.0.50727 SP2
> Installed Edition: VC Express
> I use this version of Matlab:
> MATLAB Version 7.8.0.347 (R2009a)
> and i use this operating System: Microsoft Windows Vista Version 6.0 (Build 6001: Service Pack 1) Home premium
>
> I am trying to compile 'engwindemo.cpp' example but i take this error message:
>
> 1>Compiling...
> 1>win.cpp
> 1>.\win.cpp(39) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
> 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
> 1>.\win.cpp(104) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
> 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
> 1>.\win.cpp(113) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
> 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
> 1>.\win.cpp(122) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'LPSTR' to 'LPCTSTR'
> 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
>
> Can anyone help me?
>
> Thank you in advance!

The code is:

/*
 * engwindemo.c
 *
 * This is a simple program that illustrates how to call the MATLAB
 * Engine functions from a C program for windows
 *
 * Copyright 1984-2003 The MathWorks, Inc.
 */
/* $Revision: 1.10.4.1 $ */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libut.lib")
#pragma comment (lib,"libeng.lib")
#define BUFSIZE 256

static double Areal[6] = { 1, 2, 3, 4, 5, 6 };

int PASCAL WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszCmdLine,
                    int nCmdShow)

{
Engine *ep;
mxArray *T = NULL, *a = NULL, *d = NULL;
char buffer[BUFSIZE+1];
double *Dreal, *Dimag;
double time[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

/*
* Start the MATLAB engine
*/
if (!(ep = engOpen(NULL))) {
MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",
(LPSTR) "Engwindemo.c", MB_OK);
exit(-1);
}

/*
* PART I
*
* For the first half of this demonstration, we will send data
* to MATLAB, analyze the data, and plot the result.
*/

/*
* Create a variable from our data
*/
T = mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((char *) mxGetPr(T), (char *) time, 10*sizeof(double));

/*
* Place the variable T into the MATLAB workspace
*/
engPutVariable(ep, "T", T);

/*
* Evaluate a function of time, distance = (1/2)g.*t.^2
* (g is the acceleration due to gravity)
*/
engEvalString(ep, "D = .5.*(-9.8).*T.^2;");

/*
* Plot the result
*/
engEvalString(ep, "plot(T,D);");
engEvalString(ep, "title('Position vs. Time for a falling object');");
engEvalString(ep, "xlabel('Time (seconds)');");
engEvalString(ep, "ylabel('Position (meters)');");

/*
* PART II
*
* For the second half of this demonstration, we will create another mxArray
* put it into MATLAB and calculate its eigen values
*
*/

a = mxCreateDoubleMatrix(3, 2, mxREAL);
memcpy((char *) mxGetPr(a), (char *) Areal, 6*sizeof(double));
engPutVariable(ep, "A", a);

/*
* Calculate the eigen value
*/
engEvalString(ep, "d = eig(A*A')");

/*
* Use engOutputBuffer to capture MATLAB output. Ensure first that
* the buffer is always NULL terminated.
*/
buffer[BUFSIZE] = '\0';
engOutputBuffer(ep, buffer, BUFSIZE);

/*
* the evaluate string returns the result into the
* output buffer.
*/
engEvalString(ep, "whos");
MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);

/*
* Get the eigen value mxArray
*/
d = engGetVariable(ep, "d");
engClose(ep);

if (d == NULL) {
MessageBox ((HWND)NULL, (LPSTR)"Get Array Failed", (LPSTR)"Engwindemo.c", MB_OK);
}
else {
Dreal = mxGetPr(d);
Dimag = mxGetPi(d);
if (Dimag)
sprintf(buffer,"Eigenval 2: %g+%gi",Dreal[1],Dimag[1]);
else
sprintf(buffer,"Eigenval 2: %g",Dreal[1]);
MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR)"Engwindemo.c", MB_OK);
mxDestroyArray(d);
}

/*
* We're done! Free memory, close MATLAB engine and exit.
*/
mxDestroyArray(T);
mxDestroyArray(a);

return(0);
}

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com