Calling a C code function from MATLAB main program
80 views (last 30 days)
Show older comments
I have several C functions (and *.h files). I would like to be able to call the functions from MATLAB code and display the result with MATLAB functions that do the same thing.
I am using Windows and have Microsoft Visual Studio 2022 installed on the same computer using MATLAB. I have tried mex commands, but keep getting messages that say that no compatible compiler is detected.
I have been looking online and on these forums have not found anything that explicitly answers these questions:
- Do I need a specific toolbox or app to do this?
- Why is it not detecting my MS Visual Studio, even when it's started up?
- What are the steps I need for this? Convert the *.c file to *.mex or some other format?
- Suppose I have some library or *.mex file generated from the *.c function(s); what is the format of the function calls -- the actual syntax of these lines -- that I need in my MATLAB code to access these *.c functions?
1 Comment
James Tursa
on 2 Feb 2024
Edited: James Tursa
on 2 Feb 2024
What version of MATLAB are you using? Do you want the C functions to be callable just like normal MATLAB functions?
Answers (1)
Pratyush Swain
on 2 Feb 2024
Hi Anne,
To call C functions from MATLAB, you generally need to create MEX files, you dont need a specific toolbox for this purpose. If MATLAB is not detecting your Visual Studio installation, it might be because of an unsupported version or a configuration issue, try running "mex -setup" in MATLAB to configure the compiler.You can also install a MinGW Compiler (c) compiler for C language compilation.
Below is a step by step workflow for creating a MEX function that adds two numbers, compiling it, and then calling it from MATLAB.
Step 1: Configure compiler for mex
>> mex -setup
Expected Output:
Step 2: Write the C Function
Create a new file named add_two_numbers.c with the following content:
#include "mex.h"
/* C function which will called by mex function */
double my_c_function(double a, double b){
return a+b;
}
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double a; /* input scalar */
double b; /* input scalar */
double c; /* output scalar */
/* get the value of the scalar inputs */
a = mxGetScalar(prhs[0]);
b = mxGetScalar(prhs[1]);
/* Calling C function and retreiving addition output */
c = my_c_function(a, b);
/* create the output scalar */
plhs[0] = mxCreateDoubleScalar(c);
}
To learn more on mexfunction and components of a C MEX file, refer to
Step 3: Compile the MEX Function
Open MATLAB and navigate to the directory containing your add_two_numbers.c file. Run the following command in the MATLAB Command Window:
>> mex add_two_numbers.c
If MATLAB is configured correctly with a supported C compiler, this command will compile the C code into a MEX file. On a 64-bit Windows system, the output file will be named "add_two_members.mexw64".
Expected Output:
Step 4: Call the MEX Function from MATLAB
Once the MEX file is compiled, you can call it like any other MATLAB function. Here's how you can use it to add two numbers:
>> result = add_two_numbers(3, 5);
>> result
Expected Output:
For more information on writing c functions callable from MATLAB, please refer to https://www.mathworks.com/help/matlab/call-mex-files-1.html
Hope this helps.
See Also
Categories
Find more on MATLAB Support for MinGW-w64 C/C++ Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!