writing in other languages(C,python,...)

hello
Q1:is there a way to write your code in C in matlab editor just like in matlab language?
Q2:is there a way to use .c and .h files as well as .m files in matlab command window?
I know it is possible to convert a .m file to a .mexw64 file using matlab coder, but i've seen an option in matlab preferences to choose between several languages. I chose C/C++ and hit Ok but it didn't work.

回答(1 个)

You can use the MATLAB editor to write C/C++ code. It is language sensitive.
You cannot use C/C++ (or .h) files directly from the command window. The C/C++ code must first be converted into a mex routine and compiled into a .mexw64 file, and then that file can be used from the command window (or from any m-code file). See this link to get started:
https://www.mathworks.com/help/matlab/call-mex-file-functions.html
Also, there are numerous mex routine example files in the FEX.
E.g., here is a simple mex routine that echos an input string to the screen:
/* File echo_string.c --> echos a string to the screen */
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *cp;
if( nlhs ) {
mexErrMsgTxt("This function does not produce any outputs.");
}
if( nrhs > 1 ) {
mexErrMsgTxt("Too many input arguments.");
} else if( nrhs == 1 ) {
if( mxIsChar(prhs[0]) ) {
cp = mxArrayToString(prhs[0]);
mexPrintf("%s\n",cp);
mxFree(cp);
} else {
mexErrMsgTxt("Input must be a char string.");
}
}
}
To compile and run it:
>> mex echo_string.c
>> echo_string
>> echo_string(4)
??? Error using ==> echo_string
Input must be a char string.
>> x = echo_string
??? Error using ==> echo_string
This function does not produce any outputs.
>> echo_string(2,3)
??? Error using ==> echo_string
Too many input arguments.
>> echo_string('this is a string')
this is a string

6 个评论

Alternately, .c/.h files can be used by compiling them into shared objects (.dll form) and using loadlibrary()
thanks for the answers. but both commands (mex and loadlibrary) says there's no compiler or SDK installed while i've installed the latest version of gcc compiler. is there any other compiler compatible with matlab so i can manually download and install?
E.g., see this list for R2017a
There is a link on that page for previous releases.
yes i tried microsoft visual C++ 2015 redistributable which is supported by R2016a yet matlab doesn't recognize it. as i searched for similar problems other people have a lot of workaround with that. thanks however i hope mathworks matlab installer comes with a compiler in future releases which would not require third party compiler.
Jan
Jan 2017-8-1
编辑:Jan 2017-8-1
The redistributable is just a library, which contains core functions, but not a compiler. I've installed the SDK for many years now, and they worked fluently with Matlab, when they are installed correctly. The instructions found here in the forum are short and clear, such that installing a compiler should be done a a few minutes. Unfortunately it can take hours to find the short and tidy way.
Installing older compilers for Windows 10 is a pain though.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by