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

Comments and Discussions



Hi again!
Before Xmas I had some big problems.. And I'm afraid I haven't solved them yet and I would be very grateful for any kind of answers.
Anyways, my problem is that I in my C++ program gets information from a camera and the camera functions give me matrix values on the form float. I would like to send these values straight to matlab. I haven't been able to convert the values within the C++ program to double or integer, becasue if I try to the value completely changes. So I would like to know if it's possible and if so HOW to send float values to matlab.
If it's impossible I could maybe save all my float values to a file to the disc (which my C++ program did allready at a start) and maybe call matlab from my C++ program and ask it to look into this specific file. Is this possible, and if so, How?
I'm desperate for help, it's for my masters which is soon supposed to be finished
thanks....





You can send double values to MATLAB directly. Converting float to double has not any loss. Use PutVariable to send your data (variable, vector, matrix, ...) to MATLAB. Also you can use MATLAB functions to read data from file. Just use fscanf to read from file, the syntax is very similar to C.
Also my BS project is very similar with your project. In my BS, I should get images from a camera and send data from camera to MATLAB and MATLAB process the recieved data and control two stepper motor. I use this technique for creating Video Object Tracker!
A. Riazi





Just wanted to thank you!!
I finally was able to cast my float values to double and send them to matlab. But they where supposed to be sent in a matrix as they represent image values. But the indexes were messy, I don't get why. (an element with a certain index didn't appear at the same place in the C++ program as in matlab, not even close..) So I just made a matrix with one row where I put all elements in one column each (so a vector), and now I've got it all to work...
Thanks Astrid





When compiling the original demo source code, there're two errors as below:
<br />
...\MatlabEng.cpp(44) : error C2065: 'engGetVariable' : undeclared identifier<br />
...\MatlabEng.cpp(65) : error C2065: 'engPutVariable' : undeclared identifier<br />
I cannot find the two funcs' declarations in engine.h, I don't know why, am I wrong here?
After modifying the two lines in MatlabEng.cpp from
<br />
...<br />
return (engGetVariable(pEng, name)); <br />
...<br />
return (engPutVariable(pEng, name, mp));<br />
to
<br />
...<br />
return (engGetArray(pEng, name));<br />
...<br />
return (engPutArray(pEng, mp));<br />
It's seems ok and works well.





What is your Matlab version?
A. Riazi





Sorry, I forgot this important info. My Matlab ver is:
MATLAB Version 6.1.0.450 (R12.1) on PCWIN





The above article is for MATLAB 6.5, I don't know is it compatible with MATLAB 6.1 or not!?
A. Riazi





It is. I made some minor changes to your MatLabEng.cpp class and it works great.
I added the following to the class:
// fixups for R12
#define engGetVariable engGetArray
#define engPutVariable engPutArray
And in addition, engPutArray only takes two arguments, so I fixed that:
int CMatlabEng::PutVariable(const char *name, const mxArray *mp)
{
return (engPutVariable(pEng, mp));
}
Finally, engSetVisible was not defined in R12, so I tore all functions referencing it out.
I also included <string.h> in stdafx.h, as VS.NET complained about memcpy() not being defined.
Everything works great and your wrapper is a pleasure to use. It feels much cleaner than the raw C engine API and took me about an hour to get the code I needed, written.
Thanks!





Hi,
Please post your code that I can update the article source file!
A. Riazi





And in addition, engPutArray only takes two arguments, so I fixed that:
int CMatlabEng::PutVariable(const char *name, const mxArray *mp)
{
return (engPutVariable(pEng, mp));
}
if you fixed like above, where do u put the mxArray: mp?





Hi,
How can I utilize windows vision API in MATLAB Image Processing toolbox ?






Can I send a file that I create in my C++ program to matlab? Now I understand that I can send arrays, but I would prefer sending a file instead (now on the form .pts).





Hello!
If I in my Matlab program excludes ";" after an expression I normally in Matlab can see a result in the command window. For example if I write:
[M,N] = size(thisIM)
The the values of M and N will be printed, BUT now by calling my function from C++ nothing is printed. HOW can I see this value? or CAN I see the value?
It also seems as if I can't do all normal commands I usually can do in matlab, for example "image(myImage)". Is it so that all commands can't be done via C++. (I've put this lines like image and size in a function that I now have put in the directory: C:\MATLAB6p5\bin\win32).
thanks again





You must develop your own print function or if you use mwArray or mxArray class, you can use their related print function. for mwArray use print member function and for mxArray use mlfPrint function.
Good luck,
A. Riazi





It also seems as if I can't do all normal commands I usually can do in matlab, for example "image(myImage)". Is it so that all commands can't be done via C++?
(I've put this lines like image and size in a function that I now have put in the directory: C:\MATLAB6p5\bin\win32).
Do you think I would be able to call own functions and see their performance if I instead use Jonathan de Halleux's stuff?
If I'm restricted to use only parts of matlab by calling matlab from C++, can I somewhere see WHICH FUNCTIONS I CAN call? But I suppose at the same time that Matlab still performs what I like it to perform, but it's just that it doesn't display what I hope of, is this right?
I show you a piece of matlab code here:
function displayMatrix(myMatrix)<br />
<br />
[Mm,Nm] = size(myMatrix)<br />
plot(myMatrix)<br />
thisIM = myMatrix;<br />
figure(2)<br />
plot(thisIM)
What's strange with this is that 1. I can't see Mm and Nm written anywhere, 2. The two plots should be the same I thought, but they are not!





There is no restriction for working with Matlab Engine API and you can use all of matlab functions because from VC, every commands transfered to Matlab. In this situation, no computation was done with C++!
I don't know why two plots are different. It is amazing!
A. Riazi





Just to be a bit more clear on what I've done, I show a little bit more of my code. Cause what I want to do is only to create a matrix in C++ and then send this matrix to matlab where I want to do lots of stuff to the matrix. Finally I would also like to send back a matrix to C++ but a much smaller matrix, but I haven't worked on this yet. Anyways, here is some code of mine:
IN C++:
.<br />
.<br />
myP = mxCreateDoubleMatrix(240, 320, mxREAL);<br />
double inMyP[240][320];<br />
...<br />
inMyP[i][j] = z;<br />
...<br />
memcpy((void *)mxGetPr(myP), (void *)inMyP, sizeof(inMyP));<br />
message("Send matrix myP to matlab");<br />
matlab.PutVariable("myP", myP);<br />
matlab.EvalString("[M,N]=size(myP)");
matlab.EvalString("displayMatrix(myP);");<br />
mxDestroyArray(myP);
and in matlab it's the same as in my last mail.
Do you see if I miss out of something? Because I still don't understand why I can't display the size of the matrix neither in C++ or in the matlabwindows that pops up, as well as if I add the following to my matlab program nothing happens:
figure(1)<br />
image(thisIM)<br />
colormap(gray)<br />
axis image;
Because I also understand from your last mail that EVERYTHING should be possible to perform in matlab as usual. Is it perhaps the matrix that I send to matlab that has got a problem? Is that why you asked me to try mwArray?
thanks again
and by the way, very strangely, NOW the two plots are the same...





Notice that everything that you work on is hidden from other users. This means you can see variables that you send from VC to Matlab in Matlab environment. Consider that two programs use CMatlabEng , and in a situation, they use one variable for computing something, if Matlab doesn't protect variables of some programs from another programs, an ugly event will occure!!! For this, Matlab protects every variables of one program from other users and programs.
If you want to see what happens or examine variables that be sent to Matlab, write this code:
myP = mxCreateDoubleMatrix(5, 6, mxREAL);
double inMyP[5][6];
...
...
memcpy((void *)mxGetPr(myP), (void *)inMyP, sizeof(inMyP));
message("Send matrix myP to matlab");
matlab.PutVariable("myP", myP);
matlab.EvalString("myP=myP.*2;");
myP=matlab.GetVariable("myP");
...
for (int i=0; i<5; i++)
{
for (int j=0; j<6; j++)
printf("%f\t", inMyP[i][j]);
printf("\n");
}
mxDestroyArray(myP);
A. Riazi





Thanks a lot! I will try your advices. And I understand that the forloop you've made, I can put it before the memcpycommand, right?
Since things haven't worked so well I've been looking at using other matrixes, and now I've been trying to do a :
mwArray myMatrix;
but I get this message when compiling:
error C2065: 'mwArray' : undeclared identifier
How can this be, can't I use mwArray, but only mxArray?
I had been thinking just to make myMatrix, put values into it by doing:
myMatrix(i,j) = z;
and then use :
matlab.PutVariable
directly. It would be neat, but probably this wont work... what do you think?
And I must thank you for all your help, it means a loooot!






I just sent another mail, but now I wonder for my problem before digging too deep into stuff i You think I would be better off using mxArray or mwArray??
thanks thanks





I would like to change your code as follows:
myP = mxCreateDoubleMatrix(5, 6, mxREAL);<br />
double inMyP[5][6];<br />
...<br />
for (int i=0; i<5; i++){ <br />
for (int j=0; j<6; j++) <br />
printf("%f\t", inMyP[i][j]);
printf("\n");<br />
}<br />
...<br />
memcpy((void *)mxGetPr(myP), (void *)inMyP, sizeof(inMyP));<br />
message("Send matrix myP to matlab");<br />
matlab.PutVariable("myP", myP);<br />
matlab.EvalString("myP=myP.*2;"); <br />
...<br />
for (int i=0; i<5; i++){ <br />
for (int j=0; j<6; j++) <br />
printf("%f\t", inMyP[i][j]); <br />
printf("\n");<br />
}<br />
mxDestroyArray(myP);
and where I've written HERE I don't want to print the value onto screen, but I want to save a value to the matrix. Say like this somehow:
for (int i=0; i<5; i++){ <br />
for (int j=0; j<6; j++) <br />
z = getValueFromMySpecialC++Program(); <br />
inMyP[i][j] = z; <br />
}
Or can't I do like this??
Many thanks again for your great help!





one more question on your code here. When I've got this new myP from myP=matlab.GetVariable("myP");
then how can I print the values of this myP? inMyP must now it is myP somehow... do you follow my question?
thanks





Okey... matlab hides stuff...
well, I saw now that for the matrix myP that I send to matlab I can look at its' size and values directly in the matlab command window, put it's just when I want to display something from a file that I can't...
BUT now I seem to have a problem. In my C++ program I look at the value for certain elements in inMyP and then when I try to look at the same elements in myP in matlab I don't get the same result... in matlab I just get zero. What can I have done wrong when sending the data to matlab. I must have some kind of problem with my matrix, is it maybe because it's not just an array, but a 2D matrix?







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.

