Rainfall Data Output to .txt File

  • Thread starter Thread starter sosolid
  • Start date Start date
  • Tags Tags
    Data File Output
AI Thread Summary
The discussion focuses on issues with outputting rainfall data to a .txt file in a two-column format. The user is currently experiencing problems where the data is being printed in a single column instead of the desired two columns for year and rainfall. Suggestions include using the correct format specifiers in the fprintf function, particularly ensuring that the data types match the format (e.g., using %10.2f for floats). Additionally, there is an emphasis on the need for complete code and sample data to better diagnose the issue, as the problem may stem from how the year, month, and rainfall vectors are defined. The conversation highlights the importance of proper formatting and data structure in programming for accurate output.
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
Views
1K
Replies
15
Views
2K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
8
Views
596
Replies
3
Views
1K
Replies
3
Views
2K
Replies
10
Views
3K
Replies
2
Views
4K
Back
Top