Add your own alternative version
Stats
984.7K views 8.8K downloads 92 bookmarked
Posted
27 May 2003
Licenced

Comments and Discussions


You don't need libmatlb.lib or libmmfile.lib anymore for Matlab 7.0, so get rid of those. I don't know how to help you with libeng.lib, though.
Good luck.





How do you get the values from the pointer? (I'm new at C++.)
I do this:
mxArray *myP;
myP = mxCreateDoubleMatrix(1, 1, mxREAL);
int inMyP[1][1];
memcpy((void*) mxGetPr(myP), (void*) inMyP, sizeof(inMyP));
matlab.PutVariable("myP", myP);
matlab.EvalString("myP=1;");
myP = matlab.GetVariable("myP");
cout << myP << endl;
But myP isn't 1.
Thanks.





You created a double matrix: mxCreateDoubleMatrix(....)
but declared an integer array: int inMyP[1][1];
change this line to double inMyP[1][1];
Best regards,
A. Riazi





Yay! I got it to work. This is the code, slightly modified from above. The way to fix it is to use the memcpy function, but flip the variables.
mxArray *myP = NULL;
myP = mxCreateDoubleMatrix(1, 1, mxREAL);
double inMyP[1][1];
//matlab.PutVariable("myP", myP); //Don't need.
matlab.EvalString("myP = 1");
myP = matlab.GetVariable("myP");
memcpy((void*) inMyP, (void*) mxGetPr(myP), sizeof(inMyP)); //This was the fix, notice that the variables are changed around.
cout << inMyP[0][0] << endl;
cout << endl;
mxDestroyArray(myP);
Thanks a bunch for your help.
 modified at 16:09 Wednesday 10th January, 2007





myP is a pointer to mxArray structure. Then by using
cout << myP << endl;
you will see address of pointer. For printing the content of the matrix, use mlfPrintMatrix() function.
Here is the code:
mxArray *A;
A=mxCreateDoubleMatrix(3, 3, mxREAL);
memcpy(mxGetPr(A), (void *) imMyP, 3 * 3 * sizeof(double));
mlfPrintMatrix(A);
For more info, please see my other article: Solving Engineering Problems Using MATLAB C API[^]
Best regards,
A. Riazi





Great! I might use this in the future for dealing with larger arrays.
Clement





I'm currently using MATLAB version 7.3.0.267(R2006b)
 modified at 14:58 Friday 1st December, 2006





You may change matlab.h to matrix.h, and it works. Good luck!
Prgramming





In fact, neither "matlab.h" nor "matrix.h" is needed to run this demo in Matlab 7.0! Also, "libmatlb.lib" and "libmmfile.lib" should be removed.





Plz email me if you could find the answer also
I find the following error
#include"engine.h" not found





Dear Riazi,
I'm currently building a C++ project and hoping to use the MATLAB ploting function to show the results. I already loaded your code into the project folder but there seems to be a runtime error, for example, "...undefined reference to CMatlabEng::CMatlabEng()..." I think my problem is how to intergrate/compile your code with mine. In other words, I don't know how to put your code into my makefile.
Please, Riazi, if you have any suggestion or know of any way to make this work, please help.
Thank you very much.
Nik.





As I know, MATLAB Engin API only works under windows. Because the API uses Automation (ActiveX) technology.
Best regards,
A. Riazi





can you tell me how to embed the matlab figure into winform,like list control?
i use microsoft vc#2003 and matlab r2006a.
thank you very much.
tom





Hi, I need call some functions of the Intel Library OpenCV from matlab.I'm doing a project with matlab about image processing. My project director told me that there was a direct form for call this Intel library but now it's not possible.then, I'm thinking do it by a MEXfiles and need help!!!
How can I do it?
Thank you very much!!!!
Pedro Fernandez.





There are two ways:
1 Writing your own MEX files.
2 Create an ActiveX control containing the OpenCV codes then using the control inside your mfiles.
Best regards,
A. Riazi





Hi Mr. Riazi,
I want to use your program, I add the lib like that?
#pragma comment(lib, "C:\MATLAB701\extern\lib\win32\lcc\libeng.lib")
#pragma comment(lib, "C:\MATLAB701\extern\lib\win32\lcc\libmx.lib")
#pragma comment(lib, "C:\MATLAB701\extern\lib\win32\lcc\libmatlb.lib")
#pragma comment(lib, "C:\MATLAB701\extern\lib\win32\lcc\libmat.lib")
#pragma comment(lib, "C:\MATLAB701\extern\lib\win32\lcc\libmmfile.lib")
cause my matlab lib are in C:\MATLAB701\extern\lib\win32\lcc.
but I got this error while I compiled the project:
Compiling...
MatlabClass.cpp
c:\testmatlab\eng_demo\matlabclass.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/MatlabClass.pch': No such file or directory
Error executing cl.exe.
MatlabClass.obj  1 error(s), 0 warning(s)
Your commnets and help is highly appreciated.
Best,
Reza





Salam
The error is because of the precompiled header file (*.pch). Try to clean your build and compile the project again.
Best regards,
A. Riazi





Hi
I read your article about MATLAB.
I, like Steven Romme, would also like to know if this can be used on a simulink model?
I have a simulink model of an aircraft dynamics and would like to extract variables from the simulink model using C++ and feed them into Microsoft Flight Simulator so that the aircraft within the flight sim behaves like a real aircraft.
Passing the data to Microsoft Flight Simulator is the easy part, however getting the variables into my C++ program is what I'm not sure about....
Any advice would be much appreciated.
Thanks, Marc





Hi,
I haven't worked with Simulink yet but I know that there is a solution. I think you can use Realtime workshop toolbox to link Simulink models to your VC++ apps.
Best regards,
A. Riazi





I have succesfully run you demonstration program under Matlab 6.5.0, but have now been upgraded to 7.0.4 (R14 service pack 2), which casues missing header errors. In a previous message, you mentioned that matlab.h needed to be replaced with mclmcr.h for the files to run under Matlab 7. However, neither file exists in my current version. Do you know if the headers been changed yet again, and what is needed now?
Thanks,
James





In fact, neither "matlab.h" nor "matrix.h" is needed to run this demo in Matlab 7.0! Also, "libmatlb.lib" and "libmmfile.lib" should be removed.
Good luck.
Zhang





Hi,
I read your article with great interest and i was wondering if this works with a simulink model as well.
We want to use a control system diagram designed in simulink in a standalone VC++ (MS studio 6 or .NET 2005)
This system has several inputs and outputs and a lot of parameters. What we want to do is create a VC++ program that receives the input from another module over a UDP communication and sends the output back to this module over UDP.
Is it possible to use this simulink model in your suggested way as .m files or .mex files work?
Thank in advance for your reaction!
steven.romme@gmail.com
Steven Romme
 modified at 18:15 Wednesday 22nd March, 2006





Hi,
I sent an email. Sorry that I couldn't reply you soon because I'm in trip. Here is Nowrouz (New days) that are first days of Iranian year.
Best regards,
A. Riazi





Hi,
Thanks firstly for your article and the demos, I found it quite useful!
The problem that I am having currently is to do with calling my own functions in matlab, for example:
I have made a Mfile in matlab called "times_two.m" and inside:
function y = times_two(x)<br />
y = 2*x;
and now I have got your Matlab Engine code working inside my VC++ already, HOWEVER the problems occurs when I tried doing this:
//assume I have all the libraries included
int main(int argc, char* argv[])<br />
{<br />
CMatlabEng matlab; <br />
matlab.Open(NULL);<br />
matlab.EvalString("D = 100;");
<br />
matlab.EvalString("D = times_two(100);");<br />
mxArray myVal = matlab.GetVariable("D");<br />
myPrintingFunction(myVal);<br />
<br />
matlab.Close();<br />
<br />
return 0;<br />
}<br />
Now you would EXPECT the print out of the result will be 200, however it will just print 100 and I have tried many values on it and finally i have track down to the line:
matlab.EvalString("D = times_two(100);");
It seems as if it does not return the value to my variable "D", is it because Matlab Engine do not support userdefined functions or Mfiles? I am pretty sure that the function have been executed, just that it does not return the value...
(I am using Matlab 7 R14  the latest release)
Does anyone have a solution to this??
Thx in advance!!





Did you put your mfile in MATLAB path?
Best regards,
A. Riazi






General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

