How do I include a header file in MATLAB code?
66 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Megan O'Brien
am 17 Jul. 2017
Kommentiert: Paul
am 14 Feb. 2026 um 16:24
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 Kommentare
Akzeptierte Antwort
dpb
am 17 Jul. 2017
Bearbeitet: dpb
am 9 Dez. 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.
5 Kommentare
Walter Roberson
am 14 Feb. 2026 um 8:41
You can create a namespace https://www.mathworks.com/help/matlab/matlab_oop/namespaces.html and import it.
Paul
am 14 Feb. 2026 um 16:24
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.
Weitere Antworten (1)
Stephen23
am 13 Feb. 2026 um 2:56
Bearbeitet: Stephen23
am 13 Feb. 2026 um 3:05
Simple: write a script and call/run that at the top of your code:
Scripts are introduced here:
1 Kommentar
Hugh Stone
am 14 Feb. 2026 um 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.
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!