# Can you help me with this loop?

2 visualizzazioni (ultimi 30 giorni)
Tri Dang il 18 Giu 2021
Commentato: Tri Dang il 18 Giu 2021
Instead using sprintf (which autograde wont give me point), how to use string concatenation to generate the message?
Can you give me an example?
--------------------------
function [Hours,Minutes,Message] = MyTimeConversion (TotalMinutes)
HoursX=TotalMinutes/60;
Hours=floor(HoursX);
Minutes=(HoursX-Hours)*60;
MessageA='%d minutes are equal to %d hours and %d minutes.';
MessageB='%d minutes are equal to %d hours and %d minute.';
MessageC='%d minutes are equal to %d hour and %d minutes.';
MessageD='%d minutes are euqal to %d hour and %d minute.';
MessageE='%d minute is equal to %d hours and %d minutes.';
if Hours > 1 && Minutes >1
Message=sprintf(MessageA, TotalMinutes, Hours, Minutes)
elseif Hours > 1 && Minutes==1
Message=sprintf(MessageB, TotalMinutes, Hours, Minutes)
elseif Hours==1 && Minutes>1
Message=sprintf(MessageC, TotalMinutes, Hours, Minutes)
elseif Hours==1 && Minutes==1
Message=sprintf(MessageD, TotalMinutes, Hours, Minutes)
elseif Hours==0 && Minutes==1
Message=sprintf(MessageE, TotalMinutes, Hours, Minutes)
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

James Tursa il 18 Giu 2021
Can you use num2str?
Message = [num2str(TotalMinutes) ' minutes are equal to ' num2str(Hours) ' hours and ' num2str(Minutes) ' minutes.'];
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Tri Dang il 18 Giu 2021
let me try
Thank you for the suggestion

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by