Filter löschen
Filter löschen

Changing scientific notation to long format

5 Ansichten (letzte 30 Tage)
Aaron Staszewski
Aaron Staszewski am 5 Aug. 2020
Kommentiert: Star Strider am 6 Aug. 2020
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 Kommentare
Stephen23
Stephen23 am 5 Aug. 2020
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 am 5 Aug. 2020
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 5 Aug. 2020
Select an appropriate option (such as long g) with the format function.
  2 Kommentare
Aaron Staszewski
Aaron Staszewski am 6 Aug. 2020
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 am 6 Aug. 2020
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
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by