Possible Duplicate:
Constants in MATLAB
As an interpreted language, MATLAB doesn't really have any support for compile-time constants such as macros in C or final
fields in Java. So what is the best way to declare constants, especially when they are to be heavily accessed?
Couple of ideas I've tried but none has been found satisfactory performance-wise:
Persistent variables
persistent CONSTANT;
if isempty(CONSTANT)
CONSTANT = 42;
end
This works and keeps the CONSTANT
variable local in scope. However, on my machine the persistent
declaration takes a non-negligible performance penalty.
Global variables
Some global file:
global CONSTANT;
CONSTANT = 42;
Local file:
global CONSTANT;
This works but uses constant variables, with all the usual bad things that can happen.
Any other ideas? What is the MATLAB equivalent of const
?