Can someone help me with this function assignment

조회 수: 1 (최근 30일)
Tri Dang
Tri Dang 2021년 6월 17일
댓글: Rena Berman 2021년 6월 29일
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
Rena Berman
Rena Berman 2021년 6월 29일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (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에서 Manage Products에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by