Josh E.
Josh E.

Reputation: 69

Separate definition of constant values and dependent parameters in Matlab

In my code, I have a lot of constant values and parameters that take significant space in my code.

For example in C++, I would make a header and a separate file where I would define these parameters as, e.g., "const-type" and share the header with main or other .cpp files.

How do you keep such structuring in MATLAB, is it worth it?

An example: Coefficients.m looks as follows:

classdef coefficients
    properties(Constant) 
        % NIST data
        A_N = 28.98641;
    end
end

Another file: Gas.m where I would like to use A_N looks as follows:

function Gas()
  clear all
  clc
  
  import Coefficients.* % Does not work

  % A simple print
  Values.A_N        % Does not work
  coefficients.A_N  % Does not work
  Constant.A_N      % Does not work
end

Upvotes: 1

Views: 248

Answers (2)

Hoki
Hoki

Reputation: 11812

Ok so assuming the class coefficients defined as:

classdef coefficients
    properties(Constant) 
        % NIST data
        A_N = 28.98641;
    end
end

This code must be saved in a file named coefficients.m (The class name and file name have to match to avoid weird effect sometimes).

Then assuming the following Gas.m file:

function Gas()

    % Usage with "disp()"
    disp('The A_N coefficient taken from NIST data is:')
    disp(coefficients.A_N)
    
    % Usage with fprintf
    fprintf('\nFrom NIST data, the coefficient A_N = %f\n',coefficients.A_N)
    
    % Usage in calculation (just use it as if it was a variable/constant name
    AN2 = coefficients.A_N^2 ;
    fprintf('\nA_N coefficient squared = %.2f\n',AN2)
    
    % If you want a shorter notation, you can copy the coefficient value in
    % a variable with a shorter name, then use that variable later in code
    A_N = coefficients.A_N ;
    
    fprintf('\nA_N coefficient cubed = %.2f\n',A_N^3)

end

Then running this file (calling it from the command line) yields:

>> Gas
The A_N coefficient taken from NIST data is:
                  28.98641

From NIST data, the coefficient A_N = 28.986410

A_N coefficient squared = 840.21

A_N coefficient cubed = 24354.73

Or if you simply need to access the coefficient in the Matlab console:

>> coefficients.A_N
ans =
                  28.98641

Now all these examples assume that the class file coefficient.m is visible in the current Matlab scope. For Matlab, it means the file must be in the MATLAB search path (or the current folder works too).

For more info about what is the Matlab search path and how it works you can read:


In your case, I would make a folder containing all these sorts of classes, then add this folder to the Matlab path, so you never have to worry again about individual script, function or program calling for it.

Upvotes: 2

James Tursa
James Tursa

Reputation: 2636

See this link for tips on using a class definition for this. Highlights of the tips are:

  • Properties can be accessed directly to get the value
  • Properties can be added that give the units of the constant (highly advised!)
  • Comments can be added that act as help text for the constants
  • The doc command automatically creates a reference page for this classdef

E.g.,

classdef myconstants
    properties(Constant)
        % g is the gravity of Earth
        % (https://en.wikipedia.org/wiki/Gravity_of_Earth)
        g = 9.8;
        g_units = 'm/s/s';
        % c is the speed of light in a vacuum
        c = 299792458;
        c_units = 'm/s';
    end
end

>> help myconstants.g

Upvotes: 1

Related Questions