Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fprintf trouble

  1. Feb 14, 2009 #1
    I am trying to write data to a .txt file and want each set of data in a column, so I want 2 columns. However I am currently only getting the entire data set in one big column. I am trying to output the year and the rainfall for that year into a .txt file. Can anyone help me with this?
     
  2. jcsd
  3. Feb 14, 2009 #2
    If you are writing in C, C++, Matlab or Java, this is how you proceed:
    fprintf() takes multiple arguments, the first of which is the file handle, followed by the format/pattern, and the variables whose values are to be printed follow.

    For example:

    int year, rainfall;
    ...
    FILE *f2=fopen("r.txt","w");
    ...
    fprintf(f2,"%4d%10d\n", year, rainfall);
    ...
    fclose(f2);

    If rainfall is a float, then the %10d should be replaced by %10.2f meaning a width of 10 positions, and 2 places after the decimal. The \n at the end of the pattern tells the program to insert an end of line, so each year starts on a new line.

    You will find plenty of references on the command fprintf if you google "fprintf", according to the language you are using.
     
  4. Feb 14, 2009 #3
    It does not work; this is my code


    if month(i)~=month(i-1);
    Totmonth=[Totmonth,month(i-1)];
    TotRain=[TotRain,sum(Rainfall)];
    Totyear=[Totyear,year(i-1)];
    F=TotRain';
    T=Totyear';
    m=[T F];
    id = fopen('TEST.txt', 'wt');
    fprintf(id,'%4d%10.2f\n',T,F);
    fclose(id);
    clear Rainfall;

    end;

    That just output the date as shown below:

    1971 1971.00
    1971 1971.00
    1971 1971.00
    1971 1971.00
    1971 1971.00
    1971 1971.00
    1972 1972.00
    1972 1972.00
    1972 1972.00
    1972 1972.00
    1972 1972.00
    1972 1972.00
    1973 1973.00
    1973 1973.00
    1973 1973.00
    1973 1973.00
    1973 1973.00
    1973 1973.00
    1974 1974.00
    1974 1974.00
    1974 1974.00
    1974 1974.00
    1974 1974.00
    1974 1974.00
    1975 69.00
    6.380000e+001 154.90
    6.490000e+001 23.30
    8.180000e+001 8.60
    9.500000e+000 85.10
    2.680000e+001 30.50
    1.014000e+002 129.20
    7.090000e+001 123.60
    1.147000e+002 73.10
    3.670000e+001 35.10
    25 95.00
    1.009000e+002 192.10
    2.222000e+002 198.60
    2.597000e+002 53.00
    1.143000e+002 108.00
    1.245000e+002 19.90
    6.790000e+001 85.00
    86 21.40
    3.390000e+001 110.50
    6.480000e+001 22.80
    1.490000e+001 22.40
    1.220000e+001 10.70
    7.910000e+001 45.30
    3.430000e+001 96.00
    7.410000e+001 241.50
     
  5. Feb 15, 2009 #4
    I understand your frustration. However, it seems to indicate to me that the problem probably lies with the calculations and not just the printf statement.
    I would have been able to help you find the problem if you had posted the complete code, or at least the partial code with some sample data.
    In the program, we do not get to know how the vectors year, month and especially Rainfall have been defined, so it is a little difficult to trace the source of the problem.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fprintf trouble
  1. Matlab fprintf (Replies: 6)

Loading...