How do I include a header file in MATLAB code?
73 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Megan O'Brien
el 17 de Jul. de 2017
Comentada: Walter Roberson
hace alrededor de 7 horas
I'm quite new to MATLAB and I'm trying to make a header file and include it in my MATLAB code. The header file contains a lot of constants and calculations that would be needed in the main code. I'm not sure first of all though how to save the header file, (should it be .m?) and also what's the line of code needed to include it in a MATLAB code. I'm only getting answers for including C/C++ header files in MATLAB. Apologies if this is a really basic question!
0 comentarios
Respuesta aceptada
dpb
el 17 de Jul. de 2017
Editada: dpb
el 9 de Dic. de 2021
It's a really basic question but Matlab m-file syntax doesn't include the facility.
One way you can simulate it is to make the information in the header into a function that is then called by the higher level function.
4 comentarios
Stephen23
hace alrededor de 3 horas
Editada: Stephen23
hace alrededor de 1 hora
"What I have done is define an M-File (NOT a function) that defines the flags:"
In MATLAB terminology what you wrote is called a script:
All files with the extension .m are M-files, although this term is not really used in the documentation.
"However, if you do a dependency analysis on your code, it thinks NULL, POINT,... are Missing Files."
Make a bug report: https://www.mathworks.com/support/contact_us.html
If users do not report a bug then 1) TMW might not know about it, thus 2) it is less likely to get fixed.
Walter Roberson
hace alrededor de 7 horas
You can create a namespace https://www.mathworks.com/help/matlab/matlab_oop/namespaces.html and import it.
Más respuestas (1)
Stephen23
el 13 de Feb. de 2026 a las 2:56
Editada: Stephen23
el 13 de Feb. de 2026 a las 3:05
Simple: write a script and call/run that at the top of your code:
Scripts are introduced here:
1 comentario
Hugh Stone
hace alrededor de 3 horas
Thanks - I know what a script is: I guess that terminology would haved been clearer. Minor brain-fade. I use a common "header script" at the top of ~30 files. The problem still exists though and I'm not sure it would be classed as a bug. I guess I can report it and hope TMW address it as an enhancement.
Ver también
Categorías
Más información sobre Adding custom doc 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!