Can someone help me with this function assignment

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2021-6-20
编辑:Stephen23 2021-6-20
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

请先登录,再进行评论。

回答(1 个)

John D'Errico
John D'Errico 2021-6-17
编辑:John D'Errico 2021-6-17
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 个评论
Tri Dang
Tri Dang 2021-6-17
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 2021-6-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by