how to read x file data (x=30file)
3 ビュー (過去 30 日間)
古いコメントを表示
---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 件のコメント
採用された回答
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 件のコメント
その他の回答 (2 件)
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
2012 年 7 月 6 日
That explains
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
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 件のコメント
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 Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!