how to read x file data (x=30file)

3 ビュー (過去 30 日間)
Soni huu
Soni huu 2012 年 7 月 5 日
---thanks to per isakson---
i have code in m file,
function cac = ReadSoniData( folder_spec, file )
sad = dir( fullfile( folder_spec, file ) ); %read all file in folder_spec
for sa = transpose( sad ) %transpose sad
fid = fopen( fullfile( folder_spec, sa.name ), 'r' ); %open file in folder_spec with sa.name
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', '' );
end
end
i dont know why the mfile just read the last file data from folder x?? if i add this code in editor;
tkh=cac{3}; %cell 3
akh=sum(tkh); %sum cell 3
curah_hujan=akh/60; %(sum cell 3)/60
n i write this code in comon windows, the result is
>>curah_hujan
and the result is
??? Undefined function or variable 'curah_hujan'.
how can be?
  13 件のコメント
Soni huu
Soni huu 2012 年 7 月 5 日
What you all edit mode I call debug mode(?). The prompt is "K>>". ok.. we cal debug mode..
Soni huu
Soni huu 2012 年 7 月 5 日
K>> dbquit all
??? Error using ==> dbquit
Too many input arguments.
all data is work..

サインインしてコメントする。

採用された回答

per isakson
per isakson 2012 年 7 月 5 日
Here are two functions. Copy them to two different files. Make a call
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
That will return a structure
>> plot( RainData(1).Rain )
>> title( RainData(1).DataFile )
====
function RainData = ReadManySoniData( folder_name, file_spec )
sad = dir( fullfile( folder_name, file_spec ) );
ii = 0;
for sa = transpose( sad )
ii = ii + 1;
RainData(ii) = ReadOneSoniData( folder_name, sa.name );
end
end
function rain_data = ReadOneSoniData( folder_name, file_name )
fid = fopen( fullfile( folder_name, file_name ), 'r' );
assert( fid >= 3 ...
, 'ReadOneSoniData:NoFileFound' ...
, 'Cannot find file "%s"' ...
, fullfile( folder_name, file_name ) )
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', '' );
rain_data.Created = datestr( now, 'yyyy-mm-dd HH:MM:SS' );
rain_data.DataFile = fullfile( folder_name, file_name );
rain_data.Rain = cac{3};
% and more as you see fit.
end
  37 件のコメント
Soni huu
Soni huu 2012 年 7 月 6 日
編集済み: Soni huu 2012 年 7 月 6 日
  • rain diagram where y= rain (cell3) x =time 24h(1440/60)
  • accumulate isi total rain_data.rain day 1 until day 30 or 31 or sum{file day 1} + sum{file day2} .... +sum{file day 30}
  • diagram 1 : top title is "Grafik Hujan Harian", y= rata hujan(mm/jm) x/axes = "waktu" 0 to 24 jam
  • diagram 2 : top title "grafich hujan bulanan", y="hari hujan (mm)" x/axes = "hari" 0 to 31 hari
  • diagram 3= "grafik hujan tahunan " y= "bulan hujan (mm)", x="bulan hujan" 0 to 12 bulan
11-02-2011 = nov-02-2011
Soni huu
Soni huu 2012 年 7 月 6 日
編集済み: Soni huu 2012 年 7 月 6 日
type diagram=line diagram. in mcr exel 2007 we say: scatter with smooth line and markers

サインインしてコメントする。

その他の回答 (2 件)

per isakson
per isakson 2012 年 7 月 6 日
Here are new versions of the two m-files. Put the previous ones in the folder \old. Try the new files
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
Does it work?
=====
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
  12 件のコメント
per isakson
per isakson 2012 年 7 月 6 日
That explains
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
Soni huu
Soni huu 2012 年 7 月 6 日
sorry i was wrong that work
>> RainData(2).DailyRain
ans =
249.4210

サインインしてコメントする。


per isakson
per isakson 2012 年 7 月 6 日
Try
ix_day = 1;
plot( datenum( RainData( ix_day ).Datevec ), RainData( ix_day ).Rain )
datetick
title( sprintf( 'This is the title for day index: %u', ix_day ) )
xlabel( 'This is a horisontal label' )
ylabel( 'This is a vertical label' )
  3 件のコメント
Soni huu
Soni huu 2012 年 7 月 6 日
per isakson
per isakson 2012 年 7 月 6 日
Have a look at the FEX contribution: Distribute figures. There are more, which does more or less the same thing. You might need it to show to bring some order in many plots.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by