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

Asked by Oliver on 19 Aug 2012
Latest activity Commented on by Oliver on 19 Aug 2012

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 Comments

Oliver on 19 Aug 2012

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.

Walter Roberson on 19 Aug 2012

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 on 19 Aug 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.

Oliver

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 19 Aug 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 Comments

Oliver on 19 Aug 2012

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.

Walter Roberson on 19 Aug 2012

I do not understand what you mean by "can't handle the Matrix to build up my GUI" ??

Oliver on 19 Aug 2012

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.

Walter Roberson

Contact us