Fprintf and supressing an ans.

조회 수: 7 (최근 30일)
Jared
Jared 2012년 2월 9일
Our class is working with Matlab and one of the questions we had as an assignment was to:
"create a function that receives an one interger argument, which is the hour of the day, and returns the hour.This assumes a 12-hour clock; so, for example, the next hour after 12 would be 1. Here is an example of calling this function: fprintf('The next hour will be %d.\n,nexthour(3)"
We were able to get it to display the hour but when we tried to use fprintf as the only way to display our answer, is still displayed an "ans" too.
The question I have would be is there a way to only have fprintf display the time and not "ans = whatever"?
Heres is the code we got to function but still give an "ans."
%This script will display a hour in the day and then display the next hour. The user will have to input the hour. Remember to ask how to get it to be in 'fprintf' only and not enter the ans too.
function Time = prob_3_chp_3_time_next_hour (x)
Time = x;
if x < 12;
Time = Time+1;
elseif x == 12;
Time = 1;
end
  댓글 수: 1
John D'Errico
John D'Errico 2012년 2월 12일
I'd be surprised (ok, disappointed) if the assignment told you to work with an "interger" argument. Sigh.

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

답변 (2개)

Jan
Jan 2012년 2월 9일
If you add a semicolon behind a command, the returning value is not displayed. Example:
a = sin(1)
b = sin(2);
sin(3)
sin(4);

Jared
Jared 2012년 2월 12일
I add a semicolons to the end yet it still displays both fprintf output and ans output. Here is another script I worked with that that is similiar that I want the same result to happen yet the trouble remains.
The first script is with with all semicolons added and the second is with all semicolons removed.
%Converts from Megawatt hours to Gigajoules.
function MWH_to_GJ = conv_power_mwh_to_gj (n)
MWH_to_GJ = n * 3.6;
fprintf ('When the Megawatts are %.2f,', n);
fprintf ('the Gigajoules will be %.2f.\n',MWH_to_GJ);
%Converts from Megawatt hours to Gigajoules.
function MWH_to_GJ = conv_power_mwh_to_gj (n)
MWH_to_GJ = n * 3.6
fprintf ('When the Megawatts are %.2f,', n)
fprintf ('the Gigajoules will be %.2f.\n',MWH_to_GJ)
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 2월 12일
Put the semi-colon on the call to conv_power_mwh_to_gj
As in
outpower = conv_power_mwh_to_gj(179.3); %notice the semi-colon

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by