# Code cuts 0's after the last integer to the right of the decimal place when a set number of decimal

Discussion in 'MATLAB' started by Matt Weller, Jan 28, 2010.

1. ### Matt WellerGuest

Hello,

I found the solution to saving a cell array that contains both strings and numbers to an ASCII file @:

http://www.mathworks.com/support/solutions/en/data/1-1190ZB/index.html?solution=1-1190ZB

and I am using the following code:
====================================================
% Lat File generation NEED TO ADD DATE!
exlat={lat_dat(1,3),lat_dat(1,1),0.01,sta_nme,'LAT','DATE'};
ex2 = cellfun(@ex_func,exlat,'UniformOutput',0);
size_ex2 = cellfun(@length,ex2,'UniformOutput',0);
str_length = max(max(cell2mat(size_ex2)));
ex3 = cellfun(@(x) ex_func2(x,str_length),ex2,'uniformoutput',0);
ex4 = cell2mat(ex3);
fid = fopen([sta_nme '.lat'],'at');

fprintf(fid,'%s %s %s %s %s %s\n',ex4(1:11),ex4(12:20),ex4(24:27),ex4(32:36),ex4(42:45),ex4(50:end));

fclose(fid);
====================================================

But of course, there is now a problem. The data in lat_dat(1,3) is of the from:

2.001791780821918
2.001794520547945
2.001797260273972
2.001800000000000

The problem is in the last row. it turns out line 2 of my code truncates zeros after the decimal. This is a problem if I need all the data to be the same length regardless if it were to display zeros. It all needs to be of the length of 2.0017671. Here is an example of how the data saves without this set form (lines 2 (col 2) and 4 (col 1) are the problems):

2001.7671 6.593 0.01 7odm LAT DATE
2001.7699 7.24 0.01 7odm LAT DATE
2001.7726 4.973 0.01 7odm LAT DATE
2001.789 6.748 1 m D

It becomes a propagating error and this won't input well into an outside program. I would be grateful for any way to either force a set number to display (0's after the last integer that is after a decimal point), or a way for greater flexibility in cutting the string in ex4 (and then forcing set numbers).

Thanks,
~Matt

Matt Weller, Jan 28, 2010