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개)

James Tursa
James Tursa 2017년 7월 30일
편집: James Tursa 2017년 7월 30일

2 개 추천

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

Walter Roberson
Walter Roberson 2017년 7월 30일
Alternately, .c/.h files can be used by compiling them into shared objects (.dll form) and using loadlibrary()
hosein Javan
hosein Javan 2017년 7월 31일
편집: hosein Javan 2017년 7월 31일
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?
James Tursa
James Tursa 2017년 7월 31일
편집: Walter Roberson 2017년 8월 2일
E.g., see this list for R2017a
There is a link on that page for previous releases.
hosein Javan
hosein Javan 2017년 8월 1일
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.
Walter Roberson
Walter Roberson 2017년 8월 2일
Installing older compilers for Windows 10 is a pain though.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

질문:

2017년 7월 30일

댓글:

2017년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by