how to make a constant global so all packages files use it?

53 ビュー (過去 30 日間)
Ano
Ano 2017 年 5 月 23 日
コメント済み: Rik 2021 年 8 月 5 日
hello! I would like to define a constant in such a way all my functions in the package folders use it , how can I do it ?? Thank you in advance .
  1 件のコメント
Stephen23
Stephen23 2017 年 5 月 23 日
編集済み: Stephen23 2017 年 5 月 23 日
The MATLAB documentation clearly states "Best Practice: Passing Arguments", which is also trivially easy:
k = 1.234;
myfun(k,...)
other(k,...)
morefun(k,...)
The MATLAB documentation also says "Use global variables sparingly, if at all", which is also what many discussions on this forum also advise: https://www.mathworks.com/matlabcentral/answers/319613-how-to-use-global-variable-as-local-variable
etc, etc

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 5 月 23 日
As Stephen has explained already, passing variables as arguments is the best practice.
If you have really good reasons to define a constant globally, do not use global, but prefer a dedicated function, which provides the values as struct:
function C = myPackageConstants()
C.g = 9.81;
C.pi = 3.14159265;
C.VolumeOfHeidelbergBarrel = 221716; % liter
Then call this from all function as:
C = myPackageConstants;
This is less clear than using arguments. If it is called very often, it might matter that it has a certain runtime overhead. But in opposite to globals, you can be sure that the values are not modified anywhere, such that debugging gets horrible.
  2 件のコメント
Stephen23
Stephen23 2017 年 5 月 23 日
+1 nice solution
Steven Lord
Steven Lord 2017 年 5 月 23 日
Or if it's a single constant that is broadly usable, like pi, make it a function that returns just that one value. The pi function is built-in, but if it were written as a function file it would be just:
function P = pi
P = 3.14159265358979;

サインインしてコメントする。

その他の回答 (1 件)

Robert
Robert 2021 年 2 月 26 日
You could also define a class with only constant properties. Contrary to Jan's solution, you can access the constants directly, without having to assign all of them to a variable first.
Calling it many times does not result in runtime overhead, like Jan said might be the case for his solution.
classdef Constants
properties (Constant)
g = 9.81
k = 1.234
end
end
Reference the constants using:
Constants.g
Of course, if the Constants class is in one of your packages, you have to include its namespace in the call.
  2 件のコメント
Florian B
Florian B 2021 年 8 月 5 日
編集済み: Florian B 2021 年 8 月 5 日
Would using the reference as
x = Constants.g;
instantiate the object "Constants" though? Or is this a static property?
Rik
Rik 2021 年 8 月 5 日
It is a static property for most intents and purposes.
You could easily see if multiple calls of this type will call the constructor multiple times by putting a disp in the constructor.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by