input variable stored in matrice

1 回表示 (過去 30 日間)
oselu
oselu 2014 年 8 月 5 日
コメント済み: oselu 2014 年 8 月 5 日
Hi there I wrote a program that could calculate the distance an object falls due to gravity. I prompted the user to enter a g amount and the program would calculate the the distance. Now i would like to store the specific g of the different planets in a matrice. The user would enter the name of the planet and matlab will then from there calculate the distance fallen. could anybody assist me or guide me to the right path. Much appreciated.

採用された回答

Aurele Turnes
Aurele Turnes 2014 年 8 月 5 日
You could also use a structure named g to store the value of g for each planet instead of a matrix. For example, you create your structure as follows:
g.Earth =
g.Mars =
g.Uranus =
Then, when the user inputs a planet, you can use dynamic field naming to access the appropriate g as follows:
user_planet = 'Earth';
user_g = g.(user_planet);
The following article gives a short introduction on dynamic field naming in structures:

その他の回答 (2 件)

Michael Haderlein
Michael Haderlein 2014 年 8 月 5 日
e.g.
planets={'Earth','Moon','Mars'};
g=[9.81 1.62 3.71];
user_planet=inputdlg('Planet name: ');
planet_ID=strmatch(user_planet,planets);
if isempty(planet_ID)
msgbox('Planet not found');
else
user_g=g(planet_ID);
%further calculations
end
  1 件のコメント
oselu
oselu 2014 年 8 月 5 日
Thank You very much

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


oselu
oselu 2014 年 8 月 5 日
Thank you guys I will try all these methods. really appreciate it

カテゴリ

Help Center および File ExchangeEarth and Planetary Science についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by