How to use #inlcude <>? # is declared as an invalid character!

3 visualizaciones (últimos 30 días)
#include <stdio.h> /* matlab tells me # is an invalid character, but i thougt it belongs there*/
#include <stdlib.h> /* any suggestion? I am a mere beginner so it probably has a simple reason*/
int main()
{
}

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Abr. de 2021
MATLAB will not tell you that if your file extension is .c
  2 comentarios
Walter Roberson
Walter Roberson el 13 de Abr. de 2021
I see you Unaccepted my Answer, so you must be looking for a different solution.
#include <stdio.h> /* matlab tells me # is an invalid character, but i know for fact that it belongs there*/
MATLAB permits the # character in the following contexts:
  • In comments
  • Inside '' character vectors
  • Inside "" string scalars
The code you posted is C code (not even C++). MATLAB does not permit C code in .m (MATLAB) or .mlx (Live Script) files, except in the forms noted above. Instead if you need to #include a C header file inside MATLAB code, you need to use coder.cinclude() and you would call upon the functions using coder.ceval(); see https://www.mathworks.com/help/simulink/ug/incorporate-c-code-using-a-matlab-function-block.html
Simulink uses the same tools of coder.cinclude() and coder.ceval() inside MATLAB Function Blocks, and does not permit C code directly.
MATLAB permits linking to compiled C or C++ code, which would be stored in .c or .cpp or .C or .CPP files or .c++ or .C++ files, but not in .m files.
What is your evidence that "for certain" a #include statement belongs in a .m file ?
Justus Fassnacht
Justus Fassnacht el 14 de Abr. de 2021
Thank you for awnsering.
I saw it used that way (without context in a Course on C) and thought it'd work.
This resolved all my issues.
(Sorry for beeing too certain)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by