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?
采纳的回答
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!