how define in matlab like c++
古いコメントを表示
hi i am new on matlab, i want to know who i define Constants like gravitation. in c++ it was super easy (define g=9.8) but in matlab i don't understand it.
1 件のコメント
James Tursa
2018 年 8 月 15 日
編集済み: James Tursa
2018 年 8 月 15 日
If you mean something equivalent to the "const" keyword attribute for a variable, then no MATLAB does not have this capability for basic numeric variables.
回答 (1 件)
Fangjun Jiang
2018 年 8 月 15 日
even easier
>> g=9.8
g =
9.8000
5 件のコメント
nitzan netzer
2018 年 8 月 15 日
James Tursa
2018 年 8 月 15 日
編集済み: James Tursa
2018 年 8 月 15 日
@nitzan: This statement does not take much time in MATLAB and I wouldn't worry about it. In fact, later versions of MATLAB might even keep a reference copy of this in the background so that successive times you hit this line don't even allocate anything new ... you just get a reference to what was saved. Other parts of your code are very likely dominating your run time, so spend your effort improving that code instead of this line.
Fangjun Jiang
2018 年 8 月 15 日
All true, @James Tursa!
If you insist, you can save the following two lines as G.m and include its folder location in your MATLAB path. Then you can use "G" whenever you want.
function out=G
out=9.8;
>> a=2*G
a =
19.6000
nitzan netzer
2018 年 8 月 15 日
Walter Roberson
2018 年 8 月 15 日
編集済み: Walter Roberson
2018 年 8 月 15 日
See also https://www.mathworks.com/matlabcentral/answers/321973-how-to-have-presistent-physical-constants and https://www.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html .
That is, you can create a class that has static methods that are defined as Constant. You can also create a package of functions that return constant values and import the package as needed.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!