how to make monthly graph from daily file data
이전 댓글 표시
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
2012년 7월 6일
"from this code (daily/1 file) can u make monthly graph (30file data)??"
Put more effort in describing what you need!
채택된 답변
추가 답변 (1개)
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
Soni huu
2012년 7월 6일
per isakson
2012년 7월 6일
Regarding "y= 0 to 3000" either
- the data sum up to something close to 3000 or
- an error in the code
Reagrding (7.348 to 73484) x10^5
- some hours ago you used datetick, but not here
Soni huu
2012년 7월 6일
per isakson
2012년 7월 6일
Try
hist( [ RainData.DailyRain ], 100 )
[ mx, ixm ] = max( [ RainData.DailyRain ] );
plot( datenum( RainData( ixm ).Datevec ), RainData( ixm ).Rain )
datetick
title( RainData(ixm).DataFile )
Soni huu
2012년 7월 6일
per isakson
2012년 7월 6일
편집: per isakson
2012년 7월 6일
Try
is_huge = ( [ RainData.DailyRain ] >= 2500 );
RainData(is_huge).DataFile
total rain of eight different files exceeds 2500. Is there a problem with units? You must check the code!
Soni huu
2012년 7월 6일
per isakson
2012년 7월 6일
Remember the function, EscapeBackSlash?
Soni huu
2012년 7월 6일
per isakson
2012년 7월 6일
편집: per isakson
2012년 7월 6일
Try
>> doc datetick
.
I'm lost! You know what the numbers mean. I don't!
per isakson
2012년 7월 6일
"yes, i still save"
save what?
per isakson
2012년 7월 6일
편집: per isakson
2012년 7월 6일
My points were
- title( EscapeBackSlash( RainData(ixm).DataFile ) ) to avoid the warning and
- you shouldn't rely that much on me; you must try harder to understand what the functions do
per isakson
2012년 7월 7일
편집: per isakson
2012년 7월 7일
R2012a and I guess R2012b are improved in many respects compared to the 2004 version. The 64 bit version of Matlab can handle more memory, e.g larger arrays.
I'm happy you say that!
"why y= 0 to 3000 ": Did you figure out what unit is used for the rain in column three?
per isakson
2012년 7월 7일
It's correct. However, an alternative is
rain_data.Rainrate = cac{3}; % [mm/h]
rain_data.DailyRain = 24*mean( rain_data.Rain ); % [mm]
Pros:
- this is easier to understand. The average rain rate [mm/h] times 24 hour per day, which gives [mm].
- this will handle missing data somewhat better. Missing data will be replace by the average of available data.
- this will work with a data file with other sampling rates, e.g. 2-minute data
- the confusing quantity, RainData.Rain [mm/minute], is not visible outside the function.
Soni huu
2012년 7월 7일
per isakson
2012년 7월 7일
Assume the rain_rate varies as a sinus plus some noise. Sample it 1,2,4,8 minutes and estimate daily total. Try
tt = transpose( 1 : 1 : 60 * 24 );
rain_rate_1_min = max( 0, 7*(1+sin(8*pi*tt/(60*24))) + 4*randn(60*24,1) );
rain_rate_2_min = rain_rate_1_min( 1 : 2 : end );
rain_rate_4_min = rain_rate_1_min( 1 : 4 : end );
rain_rate_8_min = rain_rate_1_min( 1 : 8 : end );
plot( rain_rate_1_min )
total = [ 24 * mean( rain_rate_1_min )
24 * mean( rain_rate_2_min )
24 * mean( rain_rate_4_min )
24 * mean( rain_rate_8_min ) ]
The four values are close. Now, you make an analytic prof (paper and pencil).
per isakson
2012년 7월 9일
편집: per isakson
2012년 7월 9일
I guess a dat-file in 'C:\matlab7\work\org\ 2010 ' contains a line that causes this error.
There is a space after "2010" - mistake?
Hint: "(row 543, field 9)"
Soni huu
2012년 7월 9일
per isakson
2012년 7월 9일
dbstop if error
Soni huu
2012년 7월 9일
Soni huu
2012년 7월 9일
per isakson
2012년 7월 10일
편집: per isakson
2012년 7월 10일
First step: In ReadManySoniData replace
RainData = cat( 2, RainData, ReadOneSoniData( folder_name, sa.name )
by
try
RainData = cat( 2, RainData, ReadOneSoniData(folder_name,sa.name)
catch
fprintf( 'Folder: %s\nFile: %s\n', foldername, sa.name )
disp( lasterr )
end
.
Second step: Run ReadManySoniData for all data files you have. That will give you a list of files, which cause troubles, in the command window.
Third step: Inspect the files (causing trouble) with an editor. Make a list with the lines, which you think are the cause of the trouble.
Fourth step: What do these lines have in common? How can these lines be identified? By what rule?
Soni huu
2012년 7월 12일
per isakson
2012년 7월 12일
편집: per isakson
2012년 7월 13일
You know - not me - whats on line: 5 column: 76. Fix the line! I guess it is a typing error.
Make sure the Code Analyzer box is green before you run the code.
Soni huu
2012년 8월 24일
per isakson
2012년 8월 24일
Soni, good to hear that you succeeded to read the data
카테고리
도움말 센터 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!