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.
0 件のコメント
回答 (1 件)
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
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!