How can I define a set of constants to be used by many functions without having to define them in each one?

15 views (last 30 days)
Greetings...
As the title suggests, I have a set of, say 100 constants:
c1 = ..; c2 ...; ....c100 = ..;
And I am writing a project that has about 50 different functions, where each function use some of these constants.
Is there a method to define these constants only once, and let each function access them without redefine or pass?
Thank you.

Accepted Answer

Dave B
Dave B on 14 Nov 2021
Edited: Dave B on 14 Nov 2021
There are several way to organize this, here are a few ideas that help you avoid global state:
First, you could pack these 100 constants into a struct. Then you still have to pass them, but you can pass them all with one argument instead of several:
myconstants = struct('c1',100,'c2',200,'c3',pi);
func1(myconstants)
func2(x,myconstants)
And inside the functions:
function out=func1(c)
out=10*c.c1 + c.c2;
end
function out = func2(x,c)
out=c.c1*x.^2 + c.c2*x + sin(c.c3);
end
Another alternative is to take a object oriented approach. If all of these functions share some data, could you consider them methods on a class?
classdef stuffwithconstants
properties(Constant)
c1 = 100;
c2 = 1/stuffwithconstants.c1;
c3 = "something"
end
methods
function out=func1(obj)
out=100*obj.c1 +obj.c2;
end
function out=func2(obj,x)
out=repmat(obj.c3,1,x);
end
end
end
And then you'd use it like:
mystuff=stuffwithconstants;
mystuff.func1
mystuff.func2(4)
  3 Comments
Dave B
Dave B on 14 Nov 2021
Edited: Dave B on 14 Nov 2021
I would say it's better to avoid globals, here and in general. Yes it's the general advice, and it's given so often for a reason. If beginners sometimes use them and experts never use them, doesn't that make you think it's a bad idea?
Of course they exist, and are available for you if you insist, and it's entirely possible that if you do things will be fine. But they reliably make life worse not better, and it's incredibly rare that they are truly useful or necessary.
A few more alternatives:
  • If your constants are all scalars, should this be a vector c? i.e. You'd use c(1) rather than c1. Consider that it's much easier to go from c(1) to c1 than vice versa. If you're enumerating 100 things, then it's best if they're in an array, that's what arrays are for. If they're not all scalars, then a cell array would work (it just means c{1} and c{2} instead of c(1) and c(2)).
c = [1.23 4 9.2 pi]; % Each constant scalar
c = {[1 2 3] 5 magic(5)}; % Varying shapes of constants
  • You could follow the struct or vector pattern but make a function, say getconstants that returns your struct. This would save you from having to pollute your list of parameters. You just call c=getconstants; at the beginning of each function. It's unclear why global c would be better than this in any way.
function c=getconstants
c(1)=1.234;
c(2)=1.33;
c(3)=8;
% or c.something=1; c.somethingelse=2;
% or c{1}=[1 2 3]; c{2}=4; c{3}=pi;
  • You could make a function for each one, like (but your description of c1..c100 makes me think that's not appropriate here):
function a=avo
a=6.02214076e23;
% note that this means you can do avo^2+avo in your code, it looks just
% like a variable

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!