필터 지우기
필터 지우기

What is the best way to insure that all of my functions are using the same constant values?

조회 수: 24 (최근 30일)
I want to make sure that my functions are all using the same values for physical constants, like earth radius, elipsoidal flattening, etc. and avoid hard-coding a bunch of constant values in each function.
Some people advocate using a function that returns constant values, e.g.
Re = LibraryConstant('Earth_Radius');
f = LibraryConstant('Ellipsoid_flattening');
ev = LibraryConstant('electron_volt');
...
The function "LibraryConstant" is a big case-select structure that returns the requested value.
Or, you might define a bunch of global constants, but this seems like an undesirable approach.
What about creating a structure or table that contains all of the constants - this would have to be passed as an additinal argumernt to each function.
What would you recommend as an efficient method?

채택된 답변

Steven Lord
Steven Lord 2019년 9월 17일
classdef myconstants
properties(Constant)
g = 9.8;
g_units = 'm/s/s';
c = 299792458;
c_units = 'm/s';
end
end
This is similar to James Tursa's struct based approach, but with one major difference: you don't need to call a function to create the struct in your workspace. You just reference the property with the name.
>> x = myconstants.c
x =
299792458
You can even give your properties help text. [This is documented in the documentation for the help function.]
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
  댓글 수: 6
Jim Riggs
Jim Riggs 2019년 9월 19일
Ah yes. I have run into that issue before here in the forum.
Jim Riggs
Jim Riggs 2019년 9월 29일
편집: Jim Riggs 2019년 9월 29일
In addition to the "doc" feature that I commented on above, I have just discovered that the auto complete function works seamlessly with the class. So, using my example of the UnitConversion class, if I type
UnitConversion.s <tab>
I get a drop-down menu of all class properties that begin with "s": {slug2lbm | slug2kg | slug2g | slug2grn } This also is a great feature of using classes.
Also, typing
UnitConversion. <tab>
will bring up a list of every property of the class. These features will be a big help in finding the value that I want.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2019년 9월 17일
I use a function that returns a structure, containing the values and the unit descriptions. Your code can either pass this structure around, or call the function. E.g.,
function e = earth
e.re = 6378.137; % equatorial radius
e.re_units = 'km';
e.we = 7.2921150e-5; % rotation rate
e.we_units = 'radians/sec';
e.flatinv = 298.257223563;
:
end
I put the units as strings so that simply displaying the structure will show me what the units are.
  댓글 수: 2
Orion Smedley
Orion Smedley 2022년 11월 15일
Would this run through the entire earth function every time you calll say earth.re? Or would it just run until it got to earth.re and then quit?
(not an issue if the constants are just numbers like this, but this may be an issue if the constants have to be numerically calculated.)
Jim Riggs
Jim Riggs 2022년 11월 15일
You would invoke the function "earth", which would define the output structure with all of the fields. You would call this function once in your script, and then refer to the structure fields as needed.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by