Calling a integer vector and cell array string in fprintf in order

4 ビュー (過去 30 日間)
Colton Weber
Colton Weber 2019 年 3 月 23 日
コメント済み: Adam Danz 2019 年 3 月 24 日
I am trying to write a code that takes a temperature in deg F and converts it to the other three units. I must then display a message that states the original temp in deg F and then states the coinciding temp in the other units with the units listed after each temp. Here is what I have so far:
% Create cell array for converted temperatures:
U={'deg C','K','deg R'}; % stores the units in a cell array
% Ask user input for temperature to be converted:
IT=input('Enter the temperature [deg F]: '); % user inters temp.
% Unit conversions for the different units:
FT=[(IT-32)/1.8;((IT-32)/1.8)+273;IT+460]; % converts deg F to other units
fprintf('The equivalent temperatures to %3.0f deg F are: \n',IT);
fprintf('%f %s. \n',FT(),U{1},U{2},U{3});
This displays the deg F correctly, but it is not displaying the correct converted temps along with their respective units. Any help would be greatly appreciated.

採用された回答

Adam Danz
Adam Danz 2019 年 3 月 23 日
FTstr = strsplit(sprintf('%.f ', FT));
txt = [FTstr(1:end-1);U];
fprintf('The equivalent temperatures to %3.0f deg F are: \n%s',IT, sprintf('%s %s\n', txt{:}))
The equivalent temperatures to 88 deg F are:
31 deg C
304 K
548 deg R
  3 件のコメント
Colton Weber
Colton Weber 2019 年 3 月 24 日
Okay I did not notice the space after the %.f . It is working correctly now. Sorry I am new to this, but thank you very much for the insight and help!
Adam Danz
Adam Danz 2019 年 3 月 24 日
Glad it worked out!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by