MatLab-Engine can't use a Variable for the GUI?

I'm stuck at using my variable "ball" from C++ in the MatLab-Engine to get my GUI running.
However by typing "whos" in die MatLab-Engine console it gives me my variable.
In Short: I create a 2D-array in C++, start the MatLab Engine via C++, create an mxArray, which will be the size of the 2D-array, and pass this mxArray to MatLab to build my GUI, which isn't working. But in the command console this Matrix is available and filled.
I just give you some Code snippets in C++:
double ball[5][7];
mxArray *m_ball;
m_ball = mxCreateDoubleMatrix(7, 5, mxREAL);
memcpy((void *)mxGetPr(m_ball), (void *)ball, sizeof(double)ball);
engPutVariable (ep, "ball", "m_ball");
Now i want to use MatLab to do some maths and show me where those balls are with the GUI and here is the never ending problem in the MatLab M-File:
ball3=rectangle('Position',[100, ball(3,3), 20,20],'Curvature',[1,1],'FaceColor','r');
It always ends at drawing the ball with the position from the variable. The Debugger gives me "Index exceeds matrix dimensions."
Does the GUI work in some other way than the rest? If i try to calculate with the Matrix in the MatLabWorkspace by typing some Commands in C++, it works perfectly.
Creating a MatFile and load it each time in MatLab works too, though it isn't the way I want it to be. The same goes with declaring the variable in the MatLab code, then it works too.
Any Tips would be great.
Oliver

4 Commenti

Jan
Jan il 19 Ago 2012
Modificato: Jan il 19 Ago 2012
You forgot to mention how you try to provide the variable m_ball to the Matlab workspace. Where do you define the variable "ball" in Matlab?
"sizeof(double)ball" looks strange. ball has the type double and I do not expect that the memcpy from (void *)ball creates what you expect.
Yes, I forgot to write the Code to give m_ball to MatLab, it's like that:
engPutVariable (ep, "ball", "m_ball");
I edited in the text...
As far as I understand, if I declare the mxArray "m_ball" to be "ball" in the MatLab workspace. If I pause the Code and ask MatLab which variables it has (with "whos") he gives me the ball as a 5x7 Matrix and the values of each element are correct too.
Please expand on "It always ends at drawing the ball with the position from the variable."
Also, when you say "The Debugger gives me "Index exceeds matrix dimensions."" then which line of code is that, and what size() is the variable and what index are you using?
Oliver
Oliver il 19 Ago 2012
Modificato: Oliver il 19 Ago 2012
Okay, I'll show the Code of my M-File and then I'll comment it:
global ball ball1 ball2 ball3 ball4 ball5;
mainwindow=figure('Name','Billard',...
'NumberTitle','Off',...
'Menubar','none',...
'Resize','off',...
'Units','pixels',...
'Position',[0,...
0.5*(1080-1000),...
1920,1080]);
axes('Parent',mainwindow,...
'Position',[0.1,0.1,0.8,0.8],...
'XLimMode','manual',...
'XLim',[0 1000],...
'YLimMode','manual',...
'YLim',[0 1000]);
drawnow;
axis equal;
axis([0,1000,...
0,1000]);
axis off;
hold on;
rectangle('Position',[0,0,...
1000,1000],...
'FaceColor','c',...
'LineStyle','none');
%Feldlinien zeichnen
line([0,1000,...
1000,0,0],...
[0,0,...
1000,1000,0],...
'LineWidth',5,...
'Color','w');
ball=rectangle('Position', [490,490,20,20],'Curvature',[1,1],...
'FaceColor','y');
ball1=rectangle('Position',[ 0, 200, 20, 20],'Curvature', [1,1],'FaceColor','y');
ball2=rectangle('Position',[ 50, 200, 20,20],'Curvature',[1,1],'FaceColor','b');
ball3=rectangle('Position',[ball(1,2), 200, 20,20],'Curvature',[1,1],'FaceColor','r');
ball4=rectangle('Position',[150, 200, 20,20],'Curvature', [1,1],'FaceColor','g');
ball5=rectangle('Position',[200, 200, 20,20],'Curvature',[1,1],'FaceColor','r');
drawnow;
Here it is, now when I start my Code from C++ everything works fine untill it comes to draw ball3. I edit the Code for reason of Debugging, in its initial state, all balls should be drawn from the 5x7 Matrix ball, which is defined in C++ and given to MatLab. The reason the Code stops drawing the Balls, is because of the Error it gets from the line where ball3 is drawn. If I just put a number for ball(1,2) (which I did for the rest of the balls) it works.
The thing I don't understand is, if I for example make a new M-File, which only has one line like:
temp = ball (1,2) + 50;
and give it back to C++ via engGetVariable, i get the correct value.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 19 Ago 2012
I am not at all sure that I understand what you are asking.
If you have defined a variable at the C level, you cannot access the variable from the MATLAB level except by returning it from the mex routine (or perhaps by calling some routine to push the variable to the MATLAB engine.) But your code appears to be trying to use the variable itself rather than calling a routine that returns a value.
You should think of mex routines as being essentially in a different workspace than the calling routine.

3 Commenti

As mentioned above, i call the MatLab Engine from C++, define a double array with 2 Dimensions, fill it with Values and want to pass it to MatLab via engPutVariable.
Now the Problem is, if i just do some maths with it, like 'engEvalString(ep, "ball (1,2) = ball (1,2) -50");' it changes the values, therefore it can handle it, but the written M-File can't handle the Matrix to build up my GUI.
I do not understand what you mean by "can't handle the Matrix to build up my GUI" ??
I want that Matrix to pass the x and y values of my balls to draw them in the MatLab GUI. And there is the problem.
I have my Matrix loaded into MatLab, I can calculate with it, can change the Values, but MatLab seems to have a problem with the "rectangle" or even the "set" function and my Matrix.

Accedi per commentare.

Richiesto:

il 19 Ago 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by