how to make monthly graph from daily file data

2 次查看(过去 30 天)
thank to per isakson
from this code (daily/1 file) can u make monthly graph (30file data)?? =====
function RainData = ReadManySoniData( folder_name, file_spec )
sad = dir( fullfile( folder_name, file_spec ) );
RainData = struct([]);
for sa = transpose( sad )
RainData = cat( 2, RainData, ReadOneSoniData( folder_name, sa.name ) );
end
[ dummy, ixs ] = sort( [ RainData(:).DayNumber ] );
RainData = RainData( ixs );
end
function rain_data = ReadOneSoniData( folder_name, file_name )
fid = fopen( fullfile( folder_name, file_name ), 'r' );
if not( fid >= 3 )
error( 'ReadOneSoniData:NoFileFound' ...
, 'Cannot find file "%s"' ...
, fullfile( folder_name, file_name ) )
end
cac = textscan( fid, '%s', 'Whitespace','', 'Delimiter','\n' );
fclose( fid );
cac = cac{:};
tmp = cellfun( 'isempty' ...
, regexp( cac, '\d{2}:\d{2}:\d{2}\s+C\s*$' ) );
isc = not( tmp );
tmp = cellfun( 'isempty' ...
, regexp( cac, '\d{2}:\d{2}:\d{2}\s+\*\*\s+----' ) );
iss = not( tmp );
cac( isc | iss ) = [];
str = transpose( char( cac ) );
nl = sprintf('\n');
str = cat( 1, str, repmat( nl(:), [length(nl),size(str,2)] ) );
cac = cell(1,9);
[cac{:}] = strread( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'delimiter', ' ', 'whitespace', '' );
try
date_vec = nan(1,3);
date_vec( [2,3,1] ) = sscanf( file_name, '%2u-%2u-%4u%*s' );
catch me
if strcmp( me.identifier, 'MATLAB:index_assign_element_count_mismatch' )
warning( 'ReadOneSoniData:CannotParseFileName' ...
, 'Cannot extract a date from file name: "%s"' ...
, file_name )
rain_data = struct([]);
return
else
rethrow( me )
end
end
str = transpose( char( cac{1} ) );
vec = nan( size(str,2), 3 );
[ vec(:,1), vec(:,2), vec(:,3) ] ...
= strread( str, '%2u:%2u:%2u', 'delimiter','','whitespace','' );
rain_data.Created = datestr( now, 'yyyy-mm-dd HH:MM:SS' );
rain_data.DataFile = fullfile( folder_name, file_name );
rain_data.Datevec = [ repmat( date_vec, [size(vec,1),1] ), vec ];
rain_data.DayNumber = datenum( date_vec );
rain_data.Rain = cac{3};
rain_data.DailyRain = sum( rain_data.Rain );
% and more as you see fit.
end
  2 个评论
per isakson
per isakson 2012-7-6
"from this code (daily/1 file) can u make monthly graph (30file data)??"
Put more effort in describing what you need!

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2012-7-6
编辑:per isakson 2012-7-6
Here is a function that returns total monthly rain. Try
>> mr = MonthlyRain( RainData );
>> plot( mr(1).Rain, 'd' );
>> bar( mr.Rain );
The values of the monthly rain could they be correct?
function monthly_rain = MonthlyRain( RainData )
day_number = [ RainData(:).DayNumber ];
month_number= month( day_number );
year_number = year( day_number );
year_list = unique( year_number );
monthly_rain = struct( 'Year', num2cell( year_list ), 'Rain', nan(12,1) );
ix_yy = 0;
for yy = year_list
is_yy = ( yy == year_number );
ix_yy = ix_yy + 1;
for mm = 1 : 12
is_mm = ( mm == month_number );
is_ym = ( is_yy & is_mm );
if any( is_ym )
monthly_rain(ix_yy).Rain(mm) = sum([RainData( is_ym ).DailyRain]);
end
end
end
end
  63 个评论
per isakson
per isakson 2012-7-13
Do you have problem reading the file, '12-19-2010.dat'?
I have successfully red the 2011 data, which I downloaded some days ago. However, I will not download more data. I've run out of time.
Add these lines
fid = fopen( 'c:\temp\ReadRainDataFailures.log', 'a' );
if fid >= 3
fprintf(fid, '%s | %s | %s\n',datestr(now),folder_name, sa.name );
fclose( fid );
end
after
% disp( le.stack(1) )
That will give you a list of the files, which cannot be red. You might want to change the name and folder of the file. Do you have a "c:\temp"?
If you run into specific problems try make a question at Answers.
Soni huu
Soni huu 2012-7-13
the code work now......... yes i have c:\temp

请先登录,再进行评论。

更多回答(1 个)

per isakson
per isakson 2012-7-6
Here is a function that collects total daily rain for one month at a time. Try
>> [ day_number, daily_rain ] = DailyRain( RainData, 2011, 11 );
>> plot( day_number, daily_rain )
>> figure, plot( day_number, daily_rain, '.' )
>> figure, plot( day_number, '.' )
As is and all that! You must check the the values. Missing data might cause surprises.
function [ day_number, daily_rain ] = DailyRain( RainData, year_number, month_number )
day_number = [ RainData(:).DayNumber ];
is_yy = ( year( day_number ) == year_number );
is_mm = ( month( day_number ) == month_number );
is_ym = ( is_yy & is_mm );
if any( is_ym )
daily_rain = [ RainData( is_ym ).DailyRain ];
day_number = [ RainData( is_ym ).DayNumber ];
else
daily_rain = [ ];
day_number = [ ];
end
end
  32 个评论
per isakson
per isakson 2012-8-24
Soni, good to hear that you succeeded to read the data

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Target Language Compiler 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by