How do I include a header file in MATLAB code?

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!

 Réponse acceptée

dpb
dpb le 17 Juil 2017
Modifié(e) : dpb le 9 Déc 2021

0 votes

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.

5 commentaires

Megan O'Brien
Megan O'Brien le 17 Juil 2017
Cheers!
The problem with that is that all the definitions in the function are local to the function. What I have done is define an M-File (NOT a function) that defines the flags: for instance an m-file called "flags.m" with the following lines:
NULL=0;
POINT=1;
LINE=2;
ARC=4;
You then call this m-file at the top of each code function that uses these flags. This works. However, if you do a dependency analysis on your code, it thinks NULL, POINT,... are Missing Files. It would be nice if there were a way to share flags and constants across a large body of code without this issue. Maybe there is and I just don't know how to do it - but a C-like header file would be ideal.
Thanks in advance for any help/advice.
Stephen23
Stephen23 le 13 Fév 2026 à 2:55
Modifié(e) : Stephen23 le 13 Fév 2026 à 4:01
"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."
If users do not report a bug then 1) TMW might not know about it, thus 2) it is less likely to get fixed.
I typically have a function that returns the common constants in a struct
flags = flagconstants();
Then used as
function out = myfunc(in)
flags = flagconstants;
% use flags.NULL, etc.
end
I don't know if that solves the dependcy analysis issue, but I think it makes myfun a bit more readable because the source of flags.NULL, etc., is clear.

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 13 Fév 2026 à 2:56
Modifié(e) : Stephen23 le 13 Fév 2026 à 3:05

0 votes

1 commentaire

Hugh Stone
Hugh Stone le 14 Fév 2026 à 1:52
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Commenté :

le 14 Fév 2026 à 16:24

Community Treasure Hunt

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

Start Hunting!

Translated by