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

4 views (last 30 days)
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.

Accepted Answer

Adam Danz
Adam Danz on 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 Comments
Colton Weber
Colton Weber on 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!

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!