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 Weller

    Matt Weller Guest


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

    and I am using the following code:
    % Lat File generation NEED TO ADD 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));


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


    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).

    Matt Weller, Jan 28, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.