Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants in MATLAB

I've come into ownership of a bunch of MATLAB code and have noticed a bunch of "magic numbers" scattered about the code. Typically, I like to make those constants in languages like C, Ruby, PHP, etc. When Googling this problem, I found that the "official" way of having constants is to define functions that return the constant value. Seems kludgey, especially because MATLAB can be finicky when allowing more than one function per file.

Is this really the best option?

I'm tempted to use / make something like the C Preprocessor to do this for me. (I found that something called mpp was made by someone else in a similar predicament, but it looks abandoned. The code doesn't compile, and I'm not sure if it would meet my needs.)

like image 321
Benjamin Oakes Avatar asked Nov 20 '09 23:11

Benjamin Oakes


People also ask

What are constant values?

Constant value is a fixed value. In Algebra, a constant is a number, or sometimes it is denoted by a letter such as a, b or c for a fixed number. For example x+2=10, here 2 and 10 are constants.

How do you define a constant parameter in MATLAB?

Click the field to the right of the input parameter name. Select Define Constant. In the field to the right of the parameter name, enter the value of the constant or a MATLAB® expression that represents the constant. The app uses the value of the specified MATLAB expression as a compile-time constant.

How do you declare a constant?

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.

What is constant syntax?

Syntax ¶ Constants can be defined using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.


2 Answers

Matlab has constants now. The newer (R2008a+) "classdef" style of Matlab OOP lets you define constant class properties. This is probably the best option if you don't require back-compatibility to old Matlabs. (Or, conversely, is a good reason to abandon back-compatibility.)

Define them in a class.

classdef MyConstants     properties (Constant = true)         SECONDS_PER_HOUR = 60*60;         DISTANCE_TO_MOON_KM = 384403;     end end 

Then reference them from any other code using dot-qualification.

>> disp(MyConstants.SECONDS_PER_HOUR)         3600 

See the Matlab documentation for "Object-Oriented Programming" under "User Guide" for all the details.

There are a couple minor gotchas. If code accidentally tries to write to a constant, instead of getting an error, it will create a local struct that masks the constants class.

>> MyConstants.SECONDS_PER_HOUR ans =         3600 >> MyConstants.SECONDS_PER_HOUR = 42 MyConstants =      SECONDS_PER_HOUR: 42 >> whos   Name             Size            Bytes  Class     Attributes    MyConstants      1x1               132  struct                 ans              1x1                 8  double               

But the damage is local. And if you want to be thorough, you can protect against it by calling the MyConstants() constructor at the beginning of a function, which forces Matlab to parse it as a class name in that scope. (IMHO this is overkill, but it's there if you want it.)

function broken_constant_use MyConstants(); % "import" to protect assignment MyConstants.SECONDS_PER_HOUR = 42 % this bug is a syntax error now 

The other gotcha is that classdef properties and methods, especially statics like this, are slow. On my machine, reading this constant is about 100x slower than calling a plain function (22 usec vs. 0.2 usec, see this question). If you're using a constant inside a loop, copy it to a local variable before entering the loop. If for some reason you must use direct access of constants, go with a plain function that returns the value.

For the sake of your sanity, stay away from the preprocessor stuff. Getting that to work inside the Matlab IDE and debugger (which are very useful) would require deep and terrible hacks.

like image 115
Andrew Janke Avatar answered Oct 05 '22 18:10

Andrew Janke


I usually just define a variable with UPPER_CASE and place near the top of the file. But you have to take the responsibly of not changing its value.

Otherwise you can use MATLAB classes to define named constants.

like image 28
Amro Avatar answered Oct 05 '22 20:10

Amro