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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Adam Danz
Adam Danz am 23 Mär. 2019
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 Kommentare
Colton Weber
Colton Weber am 24 Mär. 2019
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by