필터 지우기
필터 지우기

Changing scientific notation to long format

조회 수: 5 (최근 30일)
Aaron Staszewski
Aaron Staszewski 2020년 8월 5일
댓글: Star Strider 2020년 8월 6일
Hi all,
I have written the following program to calculate the crosswind for a runway:
function [] = crosswind()
fprintf('\n----- Crosswind -------------------------------------------------- \n');
while 1
% Numerically Defining Input Responses
yes = 1;
Yes = 1;
yEs = 1;
yeS = 1;
YEs = 1;
yES = 1;
YeS = 1;
YES = 1;
y = 1;
Y = 1;
no = 2;
No = 2;
nO = 2;
NO = 2;
n = 2;
N = 2;
%fprintf('-------------------------------------------------------');
%fprintf('\nWelcome to the crosswind component calculator. \n')
%fprintf('This funciton is not a substitute for instructor input. \n');
%fprintf('Input directions in degrees, and velocity in knots. \n\n');
% Asking for Information
runwayDesignator = input('Two digit runway identifier: ');
windDegrees = input('Direction wind is from in degrees: ');
windMagnitude = input('Wind speed in knots: ');
% Computing Information
runwayDegrees = runwayDesignator * 10;
difference = abs( runwayDegrees - windDegrees );
multiplier = sind( difference );
crossWind = abs( windMagnitude * multiplier );
% Printing the Result
if crossWind >= 20
fprintf(2, '\nThere are %d knots of crosswind for runway %d. \n', crossWind, runwayDesignator)
end
if crossWind < 20
fprintf('\nThere are %d knots of crosswind for runway %d \n', crossWind, runwayDesignator)
end
% Asking for further responses / exiting
fprintf('\nWould you like to calculate the crosswind for another runway?\n');
answerA = input('Type response here: ');
if answerA == 2
break;
end
end
end
For example to run the funciton input the numbers:
12
360
20
The funciton works, but I would like the result to be printed in non scientific notation. To do this I tried to insert a like such as:
crossWindLong = sprintf('%2.2f', crossWind);
But when I input this, and run the funciton keeping the input vairables (12, 360 and 20) the same, the function returns some garbage result.
How would I avoid this?
Thanks
  댓글 수: 2
Stephen23
Stephen23 2020년 8월 5일
Rather than defining all of those different "yes" and "no" strings just use the 's' option to return the user string:
str = input(...,'s');
which you can trivially compare using strcmpi:
if strcmpi(str,'no')
"the function returns some garbage result."
>> sprintf('%2.2f', 12)
ans = 12.00
>> sprintf('%2.2f', 360)
ans = 360.00
>> sprintf('%2.2f', 20)
ans = 20.00
dpb
dpb 2020년 8월 5일
Why are you outputting >=20 to standard error (fileID 2 as first argument to fprintf()) and <20 to normal output?
You don't show doing anything with the result -- nor what actual code must have run to get something unexpected.
Why not just replace the "%d" format string in the fprintf calls with the format you would like?

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

채택된 답변

Star Strider
Star Strider 2020년 8월 5일
Select an appropriate option (such as long g) with the format function.
  댓글 수: 2
Aaron Staszewski
Aaron Staszewski 2020년 8월 6일
I found that I would like it to round to two decimal places (format bank) but I'm not sure where to put the command to get it to work.
I would have thought to put it before the fprintf statement but I am still printing the result in scientific notation.
Where does 'format bank' need to be placed? / what am I doing wrong?
Star Strider
Star Strider 2020년 8월 6일
The format setting does not affect fprintf or similar functions. The outputs from them are completely governed by the format string.
With respect to fprintf and the others, use '%.2f' to get two decimal point rounding for all numbers. If you specify a full field width, it must include all decimal digits to the left of the decimal, as well as the decimal itself and the sign. So for example to print π to two decimal places:
fprintf('%.2f', pi)
produces:
3.14
and:
fprintf('%5.2f', pi)
produces:
3.14
(note the leading space for the sign), and:
fprintf('%10.2f', pi)
produces:
3.14
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by