• C

Calling Matlab From C

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...
Who is Participating?
PAQed with points refunded (50)

Community Support Moderator
hssumaAuthor Commented:
I have seen this link but somehow i'm getting lot of linking errors...
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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
hssumaAuthor Commented:
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..
void main()
Engine *ep;
mxArray *mp, *ans;
double *ar, *detreal;
int i,j;
mp = mxCreateDoubleMatrix(3,3,0);
  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("");
engEvalString(ep,"ans = det(A);");
ans = engGetArray(ep,"ans");
detreal = mxGetPr(ans);


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

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...
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
hssumaAuthor Commented:
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>

int main()

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

The errors encountered are

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..

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.