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

2 views (last 30 days)
#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()
{
}

Accepted Answer

Walter Roberson
Walter Roberson on 9 Apr 2021
MATLAB will not tell you that if your file extension is .c
  2 Comments
Walter Roberson
Walter Roberson on 13 Apr 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 on 14 Apr 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)

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!