Can someone help me with this function assignment

1 visualización (últimos 30 días)
Tri Dang
Tri Dang el 17 de Jun. de 2021
Comentada: Rena Berman el 29 de Jun. de 2021
I can't run this function (also autograde can't detect it and bypass, I got 0)
Do I need to change to name of the file to match with the MyTimeConversion?
There are also few other function in the same file though.
-------------------------------------------------------------------------------------------
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
  2 comentarios
Stephen23
Stephen23 el 20 de Jun. de 2021
Editada: Stephen23 el 20 de Jun. de 2021
Original question by Tri Dang retrieved from Google Cache:
Can someone help me with this function assignment
I can't run this function (also autograde can't detect it and bypass, I got 0)
Do I need to change to name of the file to match with the MyTimeConversion?
There are also few other function in the same file though.
-------------------------------------------------------------------------------------------
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
Rena Berman
Rena Berman el 29 de Jun. de 2021
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 17 de Jun. de 2021
Editada: John D'Errico el 17 de Jun. de 2021
The name of the m-file must be MyTimeConversion.m, or MATLAB will not see it as such. You cannot have other functions in the same file either, if you want to be able to call them from MATLAB too.
  2 comentarios
Tri Dang
Tri Dang el 17 de Jun. de 2021
but function coding right or wrong?
the HW is a template with multiple problems and each problem has one function
So I don't known what to do here
John D'Errico
John D'Errico el 17 de Jun. de 2021
Is the code correct? I don't really know, since I don't know exactly what you need to do. If you choose to ask, IMHO, the code seems a bit kludgy (ok, a massive kludge.) But if it works, that is irrelevant. The fact is, your question indicates that MATLAB cannot see the function, and that is why it is failing immediately. I explained why.
You should test your function in MATLAB, verifying that it returns that which is needed, and does so correctly.

Iniciar sesión para comentar.

Categorías

Más información sobre Manage Products en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by