Calling a integer vector and cell array string in fprintf in order
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Colton Weber
el 23 de Mzo. de 2019
Comentada: Adam Danz
el 24 de Mzo. de 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.
0 comentarios
Respuesta aceptada
Adam Danz
el 23 de Mzo. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!