Rainfall Data Output to .txt File

In summary, the person is trying to output data to a .txt file in two columns, but is currently getting all the data in one column. They are using the fprintf() function, but it is not working. The suggested solution is to make sure the calculations are correct, and to include the code and sample data for further assistance.
  • #1
sosolid
4
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
  • #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.
 
  • #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
 
  • #4
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.
 

1. What is the purpose of outputting rainfall data to a .txt file?

The purpose of outputting rainfall data to a .txt file is to store the data in a structured and easily readable format for further analysis or sharing with others.

2. How is rainfall data typically collected and recorded?

Rainfall data is typically collected using rain gauges, which measure the amount of precipitation that falls over a specific area. The data is then recorded manually or automatically using specialized equipment.

3. What is the significance of having rainfall data in a .txt file format?

Having rainfall data in a .txt file format allows for easy manipulation and analysis using various software or programming languages. It also ensures compatibility across different systems and platforms.

4. Can rainfall data in a .txt file be used for forecasting and predicting future weather patterns?

Yes, rainfall data in a .txt file can be used for forecasting and predicting future weather patterns. By analyzing past patterns and trends, scientists can make predictions about future rainfall and weather conditions.

5. How accurate is rainfall data output to a .txt file?

The accuracy of rainfall data output to a .txt file depends on the accuracy of the data collection methods and equipment. However, the data can be further refined and validated through statistical analysis and comparison with other sources.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
399
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
132
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top