how to read x file data (x=30file)
Mostra commenti meno recenti
---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 Commenti
per isakson
il 5 Lug 2012
per isakson
il 5 Lug 2012
Modificato: per isakson
il 5 Lug 2012
I repeat.
Hint: In the for-loop you need to assign the result, which you want to keep to an appropriate variable and make that new variable the output argument.
All the files are red and parsed. However, the results are overwritten.
per isakson
il 5 Lug 2012
Watch Doug's videos. Step through the code and add comments, which explains what each line does.
per isakson
il 5 Lug 2012
I don't understand what you try to do. I thought the function worked. The purpose of stepping through it (as demonstrated by Doug) is to understand how and why it works.
If this example is to complicated you need to step back and work for a while with simpler examples.
Does the difference between script and function cause you problems?
per isakson
il 5 Lug 2012
Have you made functions in any other programming language? Are you aware that functions have internal variables, which vanish when the function terminates. Only the output arguments are saved, i.e. passed to the caller.
per isakson
il 5 Lug 2012
It is easier to make a GUI if you have a set of small functions, which each performs a specific task. That is compared to having a large script where all the tasks are performed in sequence.
Soni huu
il 5 Lug 2012
per isakson
il 5 Lug 2012
Modificato: per isakson
il 5 Lug 2012
What do you see on the screen? I see Command Window, Editor, History Window, Workspace Window, and Current Folder. These Windows can be turned on and off from [Desktop] in the menu bar. I've forgotten exactly how it appeared in 7.?.?
In the Workspace Window you can see which variables are "alive". That is especially useful in debug mode. (There was some delay /hazzle in updating of the Workspace Window some years ago.)
The basic stuff with passing variable (arguments) I believe is close to that in Pascal.
Soni huu
il 5 Lug 2012
per isakson
il 5 Lug 2012
I referred to Matlab 7.?.? - nevermind.
What you all edit mode I call debug mode(?). The prompt is "K>>".
You can always type "whos" in the Command Window to see the variables.
Soni huu
il 5 Lug 2012
Soni huu
il 5 Lug 2012
Risposta accettata
Più risposte (2)
per isakson
il 6 Lug 2012
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 Commenti
per isakson
il 6 Lug 2012
Search "Code Cells" in the help to see if that is in your Matlab.
per isakson
il 6 Lug 2012
Replace
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
with
catch
warning( 'ReadOneSoniData:CannotParseFileName' ...
, 'Cannot extract a date from file name: "%s"' ...
, file_name )
rain_data = struct([]);
return
end
and try agian
Soni huu
il 6 Lug 2012
per isakson
il 6 Lug 2012
What is this: "rain_data(001).DailyRain"?
Did you miss to include the line
rain_data = struct([]);
per isakson
il 6 Lug 2012
Put a break point at the line
warning( 'ReadOneSoniData:CannotParseFileName' ...
Run the code and step one line at a time after the break. That will give you the reason why rain_data is not assigned anything.
per isakson
il 6 Lug 2012
Try
>> whos
Soni huu
il 6 Lug 2012
per isakson
il 6 Lug 2012
That explains
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
Soni huu
il 6 Lug 2012
per isakson
il 6 Lug 2012
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 Commenti
Soni huu
il 6 Lug 2012
Soni huu
il 6 Lug 2012
per isakson
il 6 Lug 2012
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.
Categorie
Scopri di più su Time Series Objects in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!