Search code examples
matlabnamespacesglobal-variablesconstantssuperglobals

Define global constant in Matlab


I want to define SI global prefixes in Matlab to make my code self explaining.

I defined it in startup.m:

% define SI prefix
global SI
SI.milli = 1e-3;
SI.micro = 1e-6;
% and so on..

It works fine, if I run test.m directly

% test.m file
Area = 10 * SI.micro * 4 * SI.milli % m^2

But it fails in external functions. How can I make SI visible in the name space of external functions too?


Solution

  • Scripts share the caller variable scope. Inside a function you need to write global SI as well.