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

4 vues (au cours des 30 derniers jours)
Colton Weber
Colton Weber le 23 Mar 2019
Commenté : Adam Danz le 24 Mar 2019
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.

Réponse acceptée

Adam Danz
Adam Danz le 23 Mar 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 commentaires
Colton Weber
Colton Weber le 24 Mar 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!
Adam Danz
Adam Danz le 24 Mar 2019
Glad it worked out!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by