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

  • Thread starter Thread starter davidhedoesit
  • Start date Start date
  • Tags Tags
    File Matlab Text
AI Thread Summary
The discussion revolves around creating a MATLAB script named hw11.m to read and process weather data from a text file called MonthlyHistory.html. The primary objective is to parse the file and calculate the average of the third set of numbers in each line, which represents a specific weather metric. The user is struggling with implementing a loop to achieve this averaging. An example line from the file is provided, illustrating the format of the data, which includes dates and various weather parameters separated by commas. The user suggests that the mean function could be applied to the third column of the parsed data, indicating that the data should be structured appropriately for this calculation. The conversation highlights the need for effective parsing and data manipulation techniques in MATLAB to extract and analyze the desired information.
davidhedoesit
Messages
1
Reaction score
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
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.
 

Similar threads

Replies
11
Views
3K
Replies
3
Views
2K
Replies
19
Views
1K
Replies
9
Views
4K
Replies
6
Views
3K
Back
Top