How to display format time in sprintf or similar?

22 ビュー (過去 30 日間)
Samuel Tate
Samuel Tate 2020 年 9 月 8 日
コメント済み: Samuel Tate 2020 年 9 月 8 日
% script determines the optimum cooking time for perfectly roasted pork.
% yummmy!
%input weight of roast in kg
%output time to cook roast.
clear, clc
weight = input('please enter weight of roast(kg): ');
finisht = input('please enter finish time (in decimal hours): ');
duration = 45*(weight/0.5); %45minutes per 0.5kg
timetopreheat = 10/60; %10 minutes
timeattwotendeg = 15/60; %15 minutes
timeatonefiftydeg = duration/60;
whenpreheatoven = finisht - timeatonefiftydeg - timeattwotendeg - timetopreheat;
whenputroastin = finisht - timeatonefiftydeg - timeattwotendeg;
whenturndowntoonefiftydeg = finisht - timeatonefiftydeg;
whenremoveoven = finisht;
disp(' ')
disp('Preheat oven to 210²C')
disp('Place roast on roasting dish and put in oven')
disp('Roast for 15minutes @ 210²C then')
time = sprintf('Reduce temperture to 150²C and cook for %.0f minutes', duration);
disp(time)
disp(' ')
disp('Note: the following instruction times are in in decimal hours.')
disp(' ')
time1 = sprintf('You should preheat the oven to 210²C at %f: ', whenpreheatoven);
disp(time1)
time2 = sprintf('You should put the roast in oven at %f: ', whenputroastin);
disp(time2)
time2 = sprintf('You should turn down oven to 150²C at %f: ', whenturndowntoonefiftydeg);
disp(time2)
disp(' ')
disp('OR')
disp(' ')
% convert the times above into easily readable times.
% I'd rather this times were displayed in the lines above, but I get an error message I dont not know how to overcome.
whenpreheatovenA= hours(whenpreheatoven);
whenpreheatovenA.Format = 'hh:mm';
disp(whenpreheatovenA)
whenputroastinA = hours(whenputroastin);
whenputroastinA.Format = 'hh:mm';
disp(whenputroastinA)
whenturndowntoonefiftydegA = hours(whenturndowntoonefiftydeg);
whenturndowntoonefiftydegA.Format = 'hh:mm';
disp(whenturndowntoonefiftydegA)

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 9 月 8 日
Hi,
instead of disp and sprintf, fprintf can be used more efficiently, e.g.:
fprintf('Reduce temperture to 150²C and cook for %.0f minutes \n', duration);
disp(' ')
disp('Note: the following instruction times are in in decimal hours.')
disp(' ')
fprintf('You should preheat the oven to 210²C at %f: \n', whenpreheatoven);
fprintf('You should put the roast in oven at %f: \n', whenputroastin);
fprintf('You should turn down oven to 150²C at %f: \n', whenturndowntoonefiftydeg);
  2 件のコメント
Samuel Tate
Samuel Tate 2020 年 9 月 8 日
You should turn down oven to 150²C at Error using fprintf
Unable to convert 'duration' value to 'double'.
Error in perfectcookedpork (line 56)
fprintf('You should turn down oven to 150²C at %f: ', whenturndowntoonefiftydegA);
Samuel Tate
Samuel Tate 2020 年 9 月 8 日
Above is the error I was asking for assistance with mainly.
Thank you for the suggestion to use fprintf though.

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

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by