Solved

Calling Matlab From C

Posted on 2004-10-28
594 Views
Last Modified: 2007-12-19
Hello all,
 I have a C program running on Visual Studio 6.0. The output of the program is arrays say x[n] and y[n].. i need to plot a graph for n values.. So i have exported the array to matlab and there i plot it using plot command... So, to get the plots i need to first run the C program and then execute the matlab program to get the plots.. Is there any method so that i can get the plots directly by executing the C progam... To be more precise i mean calling the matlab program from C and executing it directly through C program... Please can u give me the procedure for doing so...
TIA
-Suma
0
Question by:hssuma
    8 Comments
     
    LVL 5

    Expert Comment

    by:van_dy
    0
     

    Author Comment

    by:hssuma
    I have seen this link but somehow i'm getting lot of linking errors...
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    0
     
    LVL 2

    Expert Comment

    by:Sadrul
    you can give mcc a try: http://www-rohan.sdsu.edu/doc/matlab/toolbox/compiler/mcc.html

    using mcc, you can generate c-code from matlab-code.

    -- Adil
    0
     

    Author Comment

    by:hssuma
    I followed the instructions given in  http://www-rohan.sdsu.edu/doc/matlab/toolbox/compiler/mcc.html

    but i'm getting few errors...

    This is the program that i used..
    #include<stdio.h>
    #include<stdlib.h>
    #include"engine.h"    
    #include<string.h>
    void main()
    {
    Engine *ep;
    mxArray *mp, *ans;
    double *ar, *detreal;
    int i,j;
    mp = mxCreateDoubleMatrix(3,3,0);
     mxSetName(mp,"A");
      ar = mxGetPr(mp);
       for( i =1;i<4;i++)

                {

     

                            for (j = 1;j<4;j++)

                            {

                                        ar[i-1+3*(j-1)] = j*j*i*i;

                            }

                }

     

    ep = engOpen("");
    engPutArray(ep,mp);
    engEvalString(ep,"ans = det(A);");
    ans = engGetArray(ep,"ans");
    detreal = mxGetPr(ans);
    fgetc(stdin);
    printf("%d",deta[1]);
    engClose(ep);

    }

    but i get errors like:::::::::::::

    Compiling...
    baffle.cpp
    C:\work\baffle\baffle.cpp(11) : error C2664: 'mxCreateDoubleMatrix' : cannot convert parameter 3 from 'const int' to 'mxComplexity'
            Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
    C:\work\baffle\baffle.cpp(38) : error C2065: 'deta' : undeclared identifier
    C:\work\baffle\baffle.cpp(38) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    baffle.exe - 3 error(s), 0 warning(s)

    Can u plzz help me solve it...
    0
     
    LVL 2

    Expert Comment

    by:Sadrul
    we need to know the function prototype of  `mxCreateDoubleMatrix'. but looks like you can try:

    mp = mxCreateDoubleMatrix(3,3, (mxComplexity)0);

    the second/third errors, obviously you haven't declared a variable named `deta'. did you mean to use `detreal' here?

    -- Adil
    0
     

    Author Comment

    by:hssuma
    Why doesn't this small program work???
    I just want to see if my matlab engine is been able to open... It gives the linking errors.. i have included the libraries like giving the path i.e. MATLAB folder->extern->lib ->win32->Microsoft ->msvc6.0.

    The code is::;


    #include <stdio.h>
    #include <stdlib.h>
    #include"engine.h"    
    #include<string.h>

    int main()

    {
       Engine *ep;
       mxArray *T = NULL, *result = NULL;
    if (!(ep = engOpen("\0"))) {
          fprintf(stderr, "\nCan't start MATLAB engine\n");
          return EXIT_FAILURE;
       }
    else
      printf("Success\n");
    }

    The errors encountered are

    Linking...
    begin.obj : error LNK2001: unresolved external symbol _engOpen
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/begin.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.

    begin.exe - 3 error(s), 0 warning(s)

    Help me solve it..

    0
     

    Accepted Solution

    by:
    PAQed with points refunded (50)

    PAQ_Man
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    860 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now