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

---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 commentaires

per isakson
per isakson le 5 Juil 2012
Modifié(e) : per isakson le 5 Juil 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
per isakson le 5 Juil 2012
Watch Doug's videos. Step through the code and add comments, which explains what each line does.
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
sad = dir( fullfile(C:\matlab7\work\org\2011, '*.dat' ) ); i use coz went folow doug step, the code cant continiou. or i write dir( fullfile(C:\matlab7\work\org\2011, '*.dat' ) ): its work.. but in line 5 [cac = textscan( fid, '%s', 'Whitespace','', 'Delimiter','\n' );] its not work anymore..
i cant handle it.. :(
per isakson
per isakson le 5 Juil 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
per isakson le 5 Juil 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
per isakson le 5 Juil 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
Soni huu le 5 Juil 2012
i know a litle about delphi n pascal. i know that function have internal variables, but i cant discriminate it. please help me to make a GUI to analysis my data. i will learn ur code, coz I should be able to interpret in session
per isakson
per isakson le 5 Juil 2012
Modifié(e) : per isakson le 5 Juil 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
Soni huu le 5 Juil 2012
i work in w7 64. i see what u see. but, when i run edit modes n check the code with doug way. matlab sometimes case is not responding(when figure out)..
per isakson
per isakson le 5 Juil 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
Soni huu le 5 Juil 2012
What you all edit mode I call debug mode(?). The prompt is "K>>". ok.. we cal debug mode..
K>> dbquit all
??? Error using ==> dbquit
Too many input arguments.
all data is work..

Connectez-vous pour commenter.

 Réponse acceptée

per isakson
per isakson le 5 Juil 2012

1 vote

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 commentaires

the result is..
> clear
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
??? Undefined command/function 'assert'.
Error in ==> ReadOneSoniData at 3
assert( fid >= 3 ...
Error in ==> ReadManySoniData at 6
RainData(ii) = ReadOneSoniData( folder_name, sa.name );
i will check again
Soni huu
Soni huu le 5 Juil 2012
if u need to check i share with you the file (2M)
Sorry, assert is a new function.
assert( fid >= 3 ...
, 'ReadOneSoniData:NoFileFound' ...
, 'Cannot find file "%s"' ...
, fullfile( folder_name, file_name ) )
Replace by
if not( fid >= 3 )
error( 'ReadOneSoniData:NoFileFound' ...
, 'Cannot find file "%s"' ...
, fullfile( folder_name, file_name ) )
end
per isakson
per isakson le 5 Juil 2012
Here you can search the documentation of the latest version http://www.mathworks.se/help/techdoc/
RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
2 sad = dir( fullfile( folder_name, file_spec ) );
per isakson
per isakson le 5 Juil 2012
I don't get the message of your previous comment showing a break(?) on line 2.
Have you created any plots of rain data?
i want to plot montly rain data (sum cell 3) with time (month) in a year
per isakson
per isakson le 5 Juil 2012
Yes, but first. The code of this answer have you succeeded to make that work?
my problem is similar with..
cac = ReadSoniData2('C:\matlab7\work\org\2011', '*.dat' )
sad = dir( fullfile( folder_spec, file ) ); NO WORK
cac = ReadSoniData('C:\matlab7\work\org\2011', '*.dat' )
cac =
Columns 1 through 2 ITS WORK
the diferen of m file is ReadSoniData2 with ReadSoniData in common windows and function cac = ReadSoniData2 with function cac = ReadSoniData why mfile 1 work and mfile 2 not work
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
this new result....
K>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
K>> plot( RainData(1).Rain ) % graph work with 0 in all 1440 data
K>> title( RainData(1).DataFile )
Warning: Unable to interpret TeX string "\matlab7\work\org\2011\01-01-2011.dat".
> In ReadSoniData2 at 2
In ReadSoniData2 at 2
per isakson
per isakson le 5 Juil 2012
Put ReadSoniData2.m in a folder named "\old". That file is replaced by ReadManySoniData and ReadOneSoniData
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
still not work with title... but just one In ReadSoniData2 at 2
K>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
K>> plot( RainData(1).Rain )
K>> title( RainData(1).DataFile )
Warning: Unable to interpret TeX string "\matlab7\work\org\2011\01-02-2011.dat".
> In ReadSoniData2 at 2
Create an m-file with this function
function str = EscapeBackSlash( str )
str = strrep( str, '\', '\\' );
end
and replace
title( RainData(1).DataFile )
by
title( EscapeBackSlash( RainData(1).DataFile ) )
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
clear
K>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
K>> plot( RainData(1).Rain ); %its work data with god data
K>> title( EscapeBackSlash( RainData(1).DataFile ) ) %no error show, but i dont know what the effec
per isakson
per isakson le 5 Juil 2012
title should display the file name above the diagram.
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
ok
title( EscapeBackSlash( RainData(1).DataFile ) ) %no error show, but i dont know what the diag
is to title of table
Soni huu
Soni huu le 5 Juil 2012
the code is work but just 4 one file???
This command
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
shall return a structure array, RainData, with the size, 1xn, where n is the number of files that match 'C:\matlab7\work\org\2011', '*.dat'. Try
>> whos RainData
per isakson
per isakson le 5 Juil 2012
"to title of table" what table do you refer to?
K>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
K>> whos RainData
Name Size Bytes Class
RainData 1x362 3907396 struct array
Grand total is 497087 elements using 3907396 bytes
Soni huu
Soni huu le 5 Juil 2012
i mind is to title of graph..
Why did you say four files? Try
>> plot( RainData(100).Rain )
>> plot( RainData(200).Rain )
>> plot( RainData(300).Rain )
per isakson
per isakson le 5 Juil 2012
Modifié(e) : per isakson le 5 Juil 2012
Now try these commands without any break points; don't enter break mode
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
>> whos RainData
>> plot( RainData(100).Rain )
>> plot( RainData(200).Rain )
>> plot( RainData(300).Rain )
RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
K>> plot( RainData(100).Rain )
K>> title( EscapeBackSlash( RainData(100).DataFile ) % file 04-13-2011
plot( RainData(200).Rain )
K>> title( EscapeBackSlash( RainData(200).DataFile ) ) % file 07-22-2011
plot( RainData(300).Rain )
title( EscapeBackSlash( RainData(300).DataFile ) ) % 10-30-2011
Soni huu
Soni huu le 5 Juil 2012
it work sir... for every file, continuity to 362 file..
Why "K>>"?
Run
>> dbquit all
to get out of debug mode!
Are plots ok?
K>> dbquit all
??? Error using ==> dbquit
Too many input arguments.
all data is work..
Now RainData is the variable you should work with. cac and other variables are "private" to the functions.
More data might be needed to be added to RainData. That shall be done after
rain_data.Rain = cac{3};
% and more as you see fit.
per isakson
per isakson le 5 Juil 2012
I guess "dbquit all" is a new feature. Anyhow you should get out of debug mode (K>>) before you start a new function. You should start your functions from ">>". If you know exactly what you do you may break that rule. Try shift+F5 or read the help.
Soni huu
Soni huu le 5 Juil 2012
how accumulate to acumulate raindata (cac{3}) and than acumulate 01-01-2011 to 01-31-2011
per isakson
per isakson le 5 Juil 2012
Now make a detailed list of what you have to do finish the job. Mark everything that is not absolutely needed.
DO NOT BREAK THE TWO WORKING FUNCTIONS!
Watch demos and do exercises with GUIDE if you think you will need it. I never use GUIDE.
1. i need to plot raindata with time(24 hour not 1440 mnuts) (if posible)
2. accumulate data raindata (cell 3) => sum(cac{3})
3. plot poin 2 with 30 or 31 file data (month)
4. acumulate 30 file data (cell 3)
5. plot poin 4 with month (12 month)(cell 3)
Soni huu
Soni huu le 5 Juil 2012
Modifié(e) : Soni huu le 5 Juil 2012
i dont need cell 2,4,5,6,7,8.
per isakson
per isakson le 5 Juil 2012
Try to be more specific!
  • What type of diagram?
  • What does "accumulate" mean?
  • Should there be any text in or around the diagram?
  • etc.
per isakson
per isakson le 6 Juil 2012
Modifié(e) : per isakson le 6 Juil 2012
Does 11-02-2011.dat contain data from November or February?
Soni huu
Soni huu le 6 Juil 2012
Modifié(e) : Soni huu le 6 Juil 2012
  • 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 le 6 Juil 2012
Modifié(e) : Soni huu le 6 Juil 2012
type diagram=line diagram. in mcr exel 2007 we say: scatter with smooth line and markers

Connectez-vous pour commenter.

Plus de réponses (2)

per isakson
per isakson le 6 Juil 2012

1 vote

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 commentaires

Soni huu
Soni huu le 6 Juil 2012
Modifié(e) : Soni huu le 6 Juil 2012
>> clear all
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
plot(rain_data(001).Rain)
??? Undefined function or variable 'me'.
Error in ==> ReadOneSoniData at 27
catch me
Error in ==> ReadManySoniData at 5
RainData = cat( 2, RainData, ReadOneSoniData( folder_name, sa.name ) );
per isakson
per isakson le 6 Juil 2012
Search "Code Cells" in the help to see if that is in your Matlab.
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
>> RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
rain_data(001).DailyRain
Warning: Cannot extract a date from file name: "SoniData.dat"
> In ReadOneSoniData at 29
In ReadManySoniData at 5
??? Undefined variable "rain_data" or class "rain_data".
What is this: "rain_data(001).DailyRain"?
Did you miss to include the line
rain_data = struct([]);
Soni huu
Soni huu le 6 Juil 2012
Modifié(e) : Soni huu le 6 Juil 2012
"rain_data(001).DailyRain" i think read file 01-01-2011
no.. i write "rain_data = struct([]);"
RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
plot(rain_data.Rain)
Warning: Cannot extract a date from file name: "SoniData.dat"
> In ReadOneSoniData at 29
In ReadManySoniData at 5
??? Undefined variable "rain_data" or class "rain_data.Rain".
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.
Soni huu
Soni huu le 6 Juil 2012
Modifié(e) : Soni huu le 6 Juil 2012
i delete "SoniData.dat" coz just sample data and...
RainData = ReadManySoniData( 'C:\matlab7\work\org\2011', '*.dat' );
plot( RainData(2).Rain )
>> plot( RainData(1).Rain )
its work, graph work, need time 7 secon
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
Try
>> whos
whos RainData Name Size Bytes Class
RainData 1x362 26777704 struct array
Grand total is 3348794 elements using 26777704 bytes
>> whos Name Size Bytes Class
RainData 1x362 26777704 struct array
Grand total is 3348794 elements using 26777704 bytes
That explains
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
sorry i was wrong that work
>> RainData(2).DailyRain
ans =
249.4210

Connectez-vous pour commenter.

per isakson
per isakson le 6 Juil 2012

1 vote

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 commentaires

Soni huu
Soni huu le 6 Juil 2012
its 100% work
Soni huu
Soni huu le 6 Juil 2012
per isakson
per isakson le 6 Juil 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.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by