Calling a C code function from MATLAB main program

80 views (last 30 days)
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
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?

Sign in to comment.

Answers (1)

Pratyush Swain
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.
  2 Comments
Anne Davenport
Anne Davenport on 2 Feb 2024
Thanks much for the reply.
Sadly "mex -setup" just gives me another "Supported compiler not detected." message. So, the Microsoft Visual Studio that I have does not make it happy. No surprise there -- this is hardly the first time Visual Studio has caused a problem.
Aaaaaaah, "install a MinGW Compiler (c) compiler". Sounds so simple, especially since it's free (which does help). But I don't install anything on this computer; the admins do that. And admins don't install anything unless it is on the special list of software that has been poked and prodded and approved by persons on another level than me.
Zero trust is the word of the day for software. But it doesn't cost anything to ask. And if I'm lucky, someone else has already asked for it and gone through the painful approval process to get it on the special list. If not, it might take a little time.
Anne Davenport
Anne Davenport on 6 Feb 2024
Just a follow up.....
MinGW Compiler (c) compiler is on that special list, which reduces, but does not eliminate, the hoops to jump through for getting it. Thanks for the instructions; I'll save them for whenever I get through the obstacle course of permissions to do first.
Remember, "Zero Trust" is the motto for software, even if -- and especially if -- it's free.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!