Rainfall Data Output to .txt File

  • Thread starter Thread starter sosolid
  • Start date Start date
  • Tags Tags
    Data File Output
Click For Summary

Discussion Overview

The discussion revolves around the challenge of writing rainfall data to a .txt file in a structured format, specifically aiming for two columns representing the year and the corresponding rainfall. Participants are sharing code snippets and troubleshooting issues related to data output formatting.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks assistance with formatting data output to ensure that years and rainfall values appear in two distinct columns in a .txt file.
  • Another participant provides a code example using the fprintf function in C, suggesting the correct format specifiers for integers and floats, and emphasizes the importance of the newline character for proper formatting.
  • A participant shares their code but reports that the output is incorrect, showing repeated values instead of the intended year and rainfall pairs.
  • Another participant expresses understanding of the frustration but suggests that the issue may stem from the calculations rather than the output formatting, indicating a need for more context regarding the definitions of the variables involved.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the source of the problem, with some attributing it to the output formatting and others suggesting it may be related to the calculations or variable definitions.

Contextual Notes

The discussion lacks complete code context, particularly regarding how the variables year, month, and Rainfall are defined, which may be critical for troubleshooting the output issue.

sosolid
Messages
4
Reaction score
0
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?
 
Physics news on Phys.org
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.
 
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
 
It does not work; this is my code

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K