writing in other languages(C,python,...)
Mostrar comentarios más antiguos
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.
Respuestas (1)
James Tursa
el 30 de Jul. de 2017
Editada: James Tursa
el 30 de Jul. de 2017
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 comentarios
Walter Roberson
el 30 de Jul. de 2017
Alternately, .c/.h files can be used by compiling them into shared objects (.dll form) and using loadlibrary()
hosein Javan
el 31 de Jul. de 2017
Editada: hosein Javan
el 31 de Jul. de 2017
James Tursa
el 31 de Jul. de 2017
Editada: Walter Roberson
el 2 de Ag. de 2017
E.g., see this list for R2017a
There is a link on that page for previous releases.
hosein Javan
el 1 de Ag. de 2017
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
el 2 de Ag. de 2017
Installing older compilers for Windows 10 is a pain though.
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!