How can I force matlab to write all decimal places to a .txt file?

5 ビュー (過去 30 日間)
Alejandro Espejo
Alejandro Espejo 2018 年 7 月 13 日
回答済み: dpb 2018 年 7 月 13 日
Here is my code:
function [] = CoordinateMaker(Latitude,Longitude)
%From Ben's Coordinates %Start Point - [30.624192467326196, -96.48521972878733] %End Point - [30.635802314671512, -96.48521136466324] format long
dlat = 0.011607532673803; %~1292m
%if dlat = 0.0135 ~ 1502.813m
%This defines the latitude vector
lat = linspace(Latitude,Latitude+dlat,369); %this makes the distance between points ~3.5m
%I chose to make the distance 3.5m between points because that
%seemed to be the lowest resolution that the car updates at based on the GPS
%data that ben sent me.
%This defines the width of the uniform distribution along with some
%other options
width = 1e-5; %~1m
%1.627e-5 ~ 1.5m
%1.4e-5 ~ 1.341m
%1.2e-5 ~ 1.15m
%This
Period = dlat;
PFactor = pi/(Period/2);
for i = 1:length(lat) %this creates the longitude vector
long(i) = width*sin(lat(i)*PFactor+pi/2.55)+Longitude;
%The function is shifted left by pi/2.55 so that it starts in the
%middle and only completes the amplitude rather than 2xAmplitude
end
x1 = 30.624192467326196; %Test starting point latitude
y1 = -96.48521972878733; %Test starting point longitude
plot(lat,long,x1,y1,'ro')
% plotted x1 and y1 so that I can see where the period is starting
% relative to the starting point
ylabel('Longitude');
xlabel('Latitude');
A = [lat; long;];
fileID = fopen('Coordinates.txt','w');
fprintf(fileID,'%6s %12s\r\n','lat','long');
fprintf(fileID,'%6.2f %12.8f\r\n',A);
fclose(fileID);
end
When I write the data to a .txt file, I matlab cuts off digits past the hundredths place in the first column, but not for the second column. How can I force MATLAB to write all the digits of my variable to the 1st column in the text file?

採用された回答

dpb
dpb 2018 年 7 月 13 日
"cuts off digits past the hundredths place in the first column, but not for the second column. "
fprintf(fileID,'%6.2f %12.8f\r\n',A);
Matlab did exactly what you asked it to...you told it to write the first column with only two decimals and the second with eight. There's insufficient room allowed even in the second column for full precision of a double, however, which is 15-16 decimal digits as you've only left room for 12 including the decimal point so only 11 digits maximum and that presumes the magnitude is such that there aren't any significant digits past the last digit to the right of the decimal point which is, in general, unlikely.
It takes a lot of space to write full double precision in a float as there has to be enough digits on both sides of the decimal to cover the full dynamic range of the possible values a double can store...that's pretty-much not doable.
If you really need a specific number of digits of precision and must use a text form, use a E format with sufficient precision. If you simply want to store the data with full internal precision for transport or recovery, use stream of .mat files; much faster and smaller.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by