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

4 次查看(过去 30 天)
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 个)

James Tursa
James Tursa 2017-7-30
编辑:James Tursa 2017-7-30
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 个评论
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.
Walter Roberson
Walter Roberson 2017-8-2
Installing older compilers for Windows 10 is a pain though.

请先登录,再进行评论。

类别

Help CenterFile 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