Jonas Stein
Jonas Stein

Reputation: 7043

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?

Upvotes: 1

Views: 2284

Answers (1)

Daniel
Daniel

Reputation: 36710

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

Upvotes: 2

Related Questions