body1 = input ('ENTER THE NAME OF THE FIRST CELESTIAL BODY: ','s');
body2 = input ('ENTER THE NAME OF THE SECOND CELESTIAL BODY: ','s');
planrad = input ('ENTER THE DISTANCE BETWEEN THESE TWO BODIES IN (m): ');
massbod1 = input ('ENTER THE MASS OF (body1): ');
massbod2 = input ('ENTER THE NAME OF (body2): ');
I'm asking the user to define the above variables... How can I programm "massbod1" and "massbod2" to automatically insert the user entered text?

 採用された回答

Matt J
Matt J 2015 年 1 月 30 日

1 投票

I suspect that this is what you are trying to do
numbodies=4;
for i=1:numbodies
massbod(i)=input (['ENTER THE MASS OF (body ' num2str(i) '): '])
end

5 件のコメント

Matt J
Matt J 2015 年 1 月 30 日
編集済み: Matt J 2015 年 1 月 30 日
You can also do this kind of thing:
>> massbod = input ('ENTER THE MASSES OF ALL BODIES AS A VECTOR: ');
ENTER THE MASSES OF ALL BODIES AS A VECTOR: [ 1 2 3 4]
>> massbod
massbod =
1 2 3 4
Stephen23
Stephen23 2015 年 1 月 30 日
+1 for not trying to dynamically name variables.
Jorge Bastillo
Jorge Bastillo 2015 年 1 月 30 日
Mathematically these would work, yes. However at the end of the program I want to tell the user some values associated with the names they entered. With this in mind, these will not accomplish that.
Matt J
Matt J 2015 年 1 月 30 日
Yes it will. You just have to apply the same techniques consistently when gathering the names,
>> cbnames= input('ENTER THE NAMES OF THE CELESTIAL BODIES:')
ENTER THE NAMES OF THE CELESTIAL BODIES:{'Jupiter','Venus','Mars'}
>> massbod = input ('ENTER THE MASS OF ALL BODIES AS A VECTOR: ');
ENTER THE MASS OF ALL BODIES AS A VECTOR: [10 20 30]
Then use a loop over both to print results,
>> for i=1:3, disp(['The mass of ', cbnames{i} ' is ' num2str(massbod(i))]), end
The mass of Jupiter is 10
The mass of Venus is 20
The mass of Mars is 30
Jorge Bastillo
Jorge Bastillo 2015 年 1 月 30 日
You're right - I had to read it again. Thanks for looping back to my comment!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Geoscience についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by