Constants in MATLAB
Asked Answered
S

8

42

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.)

Scraggy answered 20/11, 2009 at 23:24 Comment(2)
“especially because MATLAB can be finicky when allowing more than one function per file.” — MATLAB does not allow more than one public function per file. There’s nothing finicky about this. You must create a new file for each function you want to define. This is weird if you’re used to other languages, but it’s really fine once you’re used to it. So you create one file for each constant you want publicly accessible. Put them in private/ if they’re internal constants used in multiple files, or as a local function if used only within one file. Even pi is defined this way.Whiz
“I'm tempted to use / make something like the C Preprocessor to do this for me.” The C++ committee is working hard to find alternatives to the last few use cases of the preprocessor in C++. Modern languages that don’t need to be compatible with C don’t have a preprocessor. I would say there’s a clear consensus that preprocessors are a bad idea.Whiz
C
21

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.

Cur answered 20/11, 2009 at 23:31 Comment(1)
I typically use a MATLAB class to hold all of my configurable parameters. This also gives you the ability to create multiple configurations and swap them in and out easily. You can even make an array of configurations and iterate through the array, running your test code on each configuration in turn.Neoclassicism
A
36

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.

Anorak answered 23/11, 2009 at 17:6 Comment(4)
I'd love to use the OOP stuff introduced in the newer versions of Matlab, but it makes things depressingly slow in our tests. (Running a simple function 100,000 times went from taking 0.0837 seconds to 2.3689 seconds when changing from nested functions to the OOP stuff.) Most of the stuff we write needs to be fairly optimized, so that overhead is quite deterring.Scraggy
I hear ya. (See the other linked Q on OOP performance.) I use it sparingly, for the same reason. You can mix and match: put constants in a classdef for organization, and keep the rest of your code in functions or old style (faster) OOP. Overhead is per call; introducing OOP doesn't make existing non-OOP code slower. Pull the constant into a local variable before entering tight loops and it may be all right. Barring that, functions returning constant values is the idiomatic pre-2008a way to do constants, and what I'd suggest.Anorak
"Deep and terrible hacks" - one of the most ominous phrases I have every heard when dealing with software. I consider myself warned!Kuopio
Trust Matlab to take something that is a simple, intrinsic part of almost every programming language and completely screw it up. The sooner Matlab dies a painful death the better IMHO.Ubald
C
21

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.

Cur answered 20/11, 2009 at 23:31 Comment(1)
I typically use a MATLAB class to hold all of my configurable parameters. This also gives you the ability to create multiple configurations and swap them in and out easily. You can even make an array of configurations and iterate through the array, running your test code on each configuration in turn.Neoclassicism
A
9

MATLAB doesn't have an exact const equivalent. I recommend NOT using global for constants - for one thing, you need to make sure they are declared everywhere you want to use them. I would create a function that returns the value(s) you want. You might check out this blog post for some ideas.

Assorted answered 21/10, 2010 at 14:9 Comment(0)
I
4

You might some of these answers How do I create enumerated types in MATLAB? useful. But in short, no there is not a "one-line" way of specifying variables whose value shouldn't change after initial setting in MATLAB.

Incus answered 20/11, 2009 at 23:28 Comment(0)
S
2

Any way you do it, it will still be somewhat of a kludge. In past projects, my approach to this was to define all the constants as global variables in one script file, invoke the script at the beginning of program execution to initialize the variables, and include "global MYCONST;" statements at the beginning of any function that needed to use MYCONST. Whether or not this approach is superior to the "official" way of defining a function to return a constant value is a matter of opinion that one could argue either way. Neither way is ideal.

Spiffing answered 20/11, 2009 at 23:56 Comment(0)
F
1

My way of dealing with constants that I want to pass to other functions is to use a struct:

% Define constants
params.PI = 3.1416;
params.SQRT2 = 1.414;

% Call a function which needs one or more of the constants
myFunction( params ); 

It's not as clean as C header files, but it does the job and avoids MATLAB globals. If you wanted the constants all defined in a separate file (e.g., getConstants.m), that would also be easy:

params = getConstants();
Florella answered 26/11, 2014 at 20:7 Comment(0)
C
1

I use a script with simple constants in capitals and include the script in other scripts that need them.

LEFT  = 1;
DOWN  = 2;
RIGHT = 3; etc.

I do not mind about these being not constant. If I write LEFT=3 then I would be plain stupid, and there is no cure against stupidity anyway, so I do not bother.

But I really hate the fact that this method clutters up my workspace with variables that I would never have to inspect. And I also do not like to use something like turn(MyConstants.LEFT) because this makes longer statements much too wide, making my code unreadable.

What I would need is not a variable but a possibility to have real pre-compiler constants. That is: strings that are replaced by values just before executing the code. That is how it should be. A constant should not have to be a variable. It is only meant to make your code more readible and maintainable. MathWorks: PLEASE, PLEASE, PLEASE. It can't be that hard to implement this...

Canara answered 8/2, 2018 at 13:4 Comment(0)
G
0

Don't call a constant using myClass.myconst without creating an instance first! Unless speed is not an issue. I was under the impression that the first call to a constant property would create an instance and then all future calls would reference that instance, (Properties with Constant Values), but I no longer believe that to be the case. I created a very basic test function of the form:

tic;
for n = 1:N
    a = myObj.field;
end
t = toc;

With classes defined like:

classdef TestObj
    properties
        field = 10;
    end
end

or:

classdef TestHandleObj < handle
    properties
        field = 10;
    end
end

or:

classdef TestConstant
    properties (Constant)
        field = 10;
    end
end

For different cases of objects, handle-objects, nested objects etc (as well as assignment operations). Note that these were all scalars; I didn't investigate arrays, cells or chars. For N = 1,000,000 my results (for total elapsed time) were:

Access(s)  Assign(s)  Type of object/call
  0.0034    0.0042    'myObj.field' 
  0.0033    0.0042    'myStruct.field'  
  0.0034    0.0033    'myVar'                   //Plain old workspace evaluation
  0.0033    0.0042    'myNestedObj.obj.field'   
  0.1581    0.3066    'myHandleObj.field'   
  0.1694    0.3124    'myNestedHandleObj.handleObj.field'   
 29.2161         -    'TestConstant.const'      //Call directly to class(supposed to be faster)
  0.0034         -    'myTestConstant.const'    //Create an instance of TestConstant
  0.0051    0.0078    'TestObj > methods'       //This calls get and set methods that loop internally
  0.1574    0.3053    'TestHandleObj > methods' //get and set methods (internal loop)

I also created a Java class and ran a similar test:

 12.18     17.53      'jObj.field > in matlab for loop'
  0.0043    0.0039    'jObj.get and jObj.set loop N times internally'

The overhead in calling the Java object is high, but within the object, simple access and assign operations happen as fast as regular matlab objects. If you want reference behavior to boot, Java may be the way to go. I did not investigate object calls within nested functions, but I've seen some weird things. Also, the profiler is garbage when it comes to a lot of this stuff, which is why I switched to manually saving the times.

For reference, the Java class used:

public class JtestObj {
    public double field = 10;

    public double getMe() {
        double N = 1000000;
        double val = 0;
        for (int i = 1; i < N; i++) {
            val = this.field;
        }

        return val;
     }

     public void setMe(double val) {
        double N = 1000000;
        for (int i = 1; i < N; i++){
            this.field = val;
        }
     }
  }

On a related note, here's a link to a table of NIST constants: ascii table and a matlab function that returns a struct with those listed values: Matlab FileExchange

Gujarati answered 24/2, 2012 at 17:57 Comment(1)
I noticed Andrew recently did an update on Matlab speed. Probably more helpful than mine: #1693929Gujarati

© 2022 - 2024 — McMap. All rights reserved.