How to Calculate the Average of the Third Column in a Text File Using Matlab?

In summary, the conversation is about creating a Matlab script called hw11.m to read in a text file of weather information, parse it, and write formatted summary information. The text file, MonthlyHistory.html, contains data for each day of the month, including temperature, humidity, and precipitation. The task at hand is to write a loop that calculates the average of the third column of the data matrix, which represents the third set of numbers in the string. An example of this number is highlighted as bold in the file.
  • #1
davidhedoesit
2
0
I want to create a Matlab script called hw11.m, which reads in a text file of weather
information, parses it, and writes formatted summary information. The textfile is called
MonthlyHistory.html
I cannot figure out how to write a loop that averages the third set of numbers in the string.
an example is the bold number ..ect
2015-10-1,33,27,20,29,24,20,96,85,70,30.51,30.40,30.13,10,9,4,10,6,,T,5,Snow,35
Code:
fid = fopen('MonthlyHistory.html');
str = fgets(fid); 

for (i=1:1:31)
  a = sscanf(str,'%s')end

the .html file is

HTML:
2015-10-1,33,27,20,29,24,20,96,85,70,30.51,30.40,30.13,10,9,4,10,6,,T,5,Snow,35
2015-10-2,37,32,27,32,27,23,92,80,67,30.35,30.31,30.28,10,10,7,7,4,,T,8,Rain-Snow,19
2015-10-3,39,36,32,35,33,29,100,90,79,30.30,30.17,30.11,10,7,0,8,3,,0.21,8,Fog-Rain,13
2015-10-4,40,37,34,38,36,33,100,96,92,30.23,30.19,30.14,2,1,0,6,0,,0.13,8,Fog-Rain,27
2015-10-5,46,38,30,38,34,30,100,91,61,30.19,30.08,29.93,10,7,0,6,2,,T,6,Fog-Rain,23
2015-10-6,47,37,26,33,29,25,96,82,44,29.92,29.88,29.83,10,10,10,6,1,,0.00,3,,42
2015-10-7,46,36,25,31,27,23,89,73,44,29.83,29.79,29.74,10,10,10,8,4,,0.00,5,,36
2015-10-8,49,39,29,29,26,24,75,61,35,29.74,29.56,29.44,10,10,10,15,9,,0.00,6,,32
2015-10-9,45,38,31,26,24,21,75,58,34,29.43,29.24,29.06,10,10,10,20,9,,0.00,6,,22
2015-10-10,38,33,28,26,21,18,73,62,45,29.21,29.10,29.05,10,10,9,13,9,,T,5,Rain,29
2015-10-11,50,38,26,26,23,20,78,60,26,29.32,29.27,29.22,10,10,10,10,7,,0.00,2,,25
2015-10-12,36,29,22,24,21,17,85,70,49,29.39,29.25,29.21,10,10,10,10,5,,0.00,3,,38
2015-10-13,35,28,21,23,21,18,92,77,44,29.94,29.66,29.41,10,10,10,5,2,,0.00,4,,27
2015-10-14,38,31,23,26,24,21,89,76,53,30.22,30.13,29.96,10,10,10,4,1,,0.00,6,,71
2015-10-15,46,33,19,29,23,17,92,77,41,30.22,30.08,29.90,10,10,10,7,2,,0.00,2,,37
2015-10-16,47,35,23,31,25,19,85,68,31,29.88,29.61,29.36,10,10,10,13,7,,0.01,5,Rain-Snow,38
2015-10-17,38,36,34,34,32,29,92,82,63,29.41,29.31,29.25,10,10,7,12,8,,0.13,8,Rain,16
2015-10-18,48,39,30,38,33,29,100,91,68,29.61,29.51,29.41,10,6,0,8,2,,T,5,Fog-Rain-Snow,161
2015-10-19,35,32,28,32,31,29,96,93,84,29.83,29.75,29.61,6,2,0,8,1,,0.14,8,Fog-Rain-Snow,47
2015-10-20,41,33,24,34,27,22,96,85,68,29.79,29.67,29.53,10,8,0,9,3,,T,4,Fog,15
2015-10-21,38,32,25,29,25,20,89,78,63,29.61,29.54,29.51,10,10,10,12,6,,0.00,6,,22
2015-10-22,34,32,30,27,24,22,85,71,55,29.84,29.72,29.62,10,10,10,9,5,,0.00,7,,360
2015-10-23,41,33,25,27,25,20,89,77,53,29.89,29.86,29.83,10,10,10,8,2,,0.00,5,,42
2015-10-24,36,30,24,25,21,19,81,73,59,29.83,29.77,29.71,10,10,10,9,6,,0.00,4,,45
2015-10-25,37,30,23,27,22,19,88,79,60,30.07,30.01,29.84,10,10,10,5,1,,0.00,4,,35
2015-10-26,33,28,23,29,25,20,92,87,79,30.08,30.00,29.90,10,10,10,5,1,,0.00,7,,56
2015-10-27,36,33,29,30,28,27,89,84,71,29.89,29.75,29.58,10,10,10,7,3,,0.00,7,,33
2015-10-28,34,29,23,26,23,20,92,78,55,29.57,29.45,29.39,10,10,10,8,4,,0.00,6,,18
2015-10-29,30,25,20,27,23,18,96,88,69,29.41,29.39,29.36,10,5,1,9,5,,0.07,8,Snow,339
2015-10-30,25,23,20,21,19,17,96,88,78,29.73,29.54,29.40,8,3,2,8,1,,0.13,8,Snow,339
2015-10-31,20,10,-1,17,9,-2,96,86,77,30.04,29.93,29.72,10,8,2,9,4,,0.01,5,Snow,308
 
Physics news on Phys.org
  • #2
It's the third column of the matrix defined by the .html file. So
Code:
mean(data[,3])
ought to do it, assuming data is the contents of the .html.
 

1. How do I read a text file in Matlab?

To read a text file in Matlab, you can use the textread function. This function takes in the name of the text file as the first input and the format of the data as the second input. It returns the data as a matrix. Alternatively, you can use the importdata function, which automatically detects the format of the data and returns it as a structure.

2. How do I specify the delimiter when reading a text file in Matlab?

The textread function has an optional third input for specifying the delimiter of the text file. You can pass in a string, such as '\t' for tab-delimited files or ',' for comma-delimited files. If you are using the importdata function, you can specify the delimiter using the 'delimiter' name-value pair argument.

3. Can I read only certain columns from a text file in Matlab?

Yes, you can specify the columns you want to read in the textread function using the fourth input, which takes in the format string. For example, if you only want to read the first and third columns, you can pass in '%f %*s %f' as the fourth input. The %f specifier indicates that the data is in floating-point format, and the %*s specifier skips the second column. Alternatively, you can use the 'selectcolumns' option in the importdata function and specify the columns you want to read as a vector.

4. How do I handle missing data when reading a text file in Matlab?

You can handle missing data in two ways when reading a text file in Matlab. The first option is to use the textread function with the fifth input, which specifies the fill value for missing data. The second option is to use the importdata function and set the 'fillingvalues' name-value pair argument to the desired fill value.

5. Can I read a text file with a header in Matlab?

Yes, you can read a text file with a header in Matlab by using the textread function with the 'headerlines' option. You can specify the number of header lines to skip as the fifth input. Alternatively, you can use the importdata function and set the 'headerlines' name-value pair argument to the number of header lines to skip.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • Programming and Computer Science
Replies
2
Views
316
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • Advanced Physics Homework Help
Replies
11
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • Programming and Computer Science
Replies
19
Views
979
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top