Can someone help me with this function assignment

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 20 Jun 2021
Edited: Stephen23 on 20 Jun 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

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 17 Jun 2021
Edited: John D'Errico on 17 Jun 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 Comments
Tri Dang
Tri Dang on 17 Jun 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 on 17 Jun 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.

Sign in to comment.

Categories

Find more on Manage Products in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!