You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to read x file data (x=30file)
3 views (last 30 days)
Show older comments
---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 Comments
per isakson
on 5 Jul 2012
per isakson
on 5 Jul 2012
Edited: per isakson
on 5 Jul 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
on 5 Jul 2012
Watch Doug's videos. Step through the code and add comments, which explains what each line does.
Soni huu
on 5 Jul 2012
Edited: Soni huu
on 5 Jul 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
on 5 Jul 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
on 5 Jul 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
on 5 Jul 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
on 5 Jul 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
on 5 Jul 2012
Edited: per isakson
on 5 Jul 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.
per isakson
on 5 Jul 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.
Accepted Answer
per isakson
on 5 Jul 2012
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 Comments
Soni huu
on 5 Jul 2012
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
per isakson
on 5 Jul 2012
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
on 5 Jul 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?
per isakson
on 5 Jul 2012
Yes, but first. The code of this answer have you succeeded to make that work?
Soni huu
on 5 Jul 2012
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
on 5 Jul 2012
Edited: Soni huu
on 5 Jul 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
on 5 Jul 2012
Put ReadSoniData2.m in a folder named "\old". That file is replaced by ReadManySoniData and ReadOneSoniData
Soni huu
on 5 Jul 2012
Edited: Soni huu
on 5 Jul 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
per isakson
on 5 Jul 2012
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
on 5 Jul 2012
Edited: Soni huu
on 5 Jul 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
on 5 Jul 2012
title should display the file name above the diagram.
per isakson
on 5 Jul 2012
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
on 5 Jul 2012
"to title of table" what table do you refer to?
per isakson
on 5 Jul 2012
Why did you say four files? Try
>> plot( RainData(100).Rain )
>> plot( RainData(200).Rain )
>> plot( RainData(300).Rain )
per isakson
on 5 Jul 2012
Edited: per isakson
on 5 Jul 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 )
Soni huu
on 5 Jul 2012
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
per isakson
on 5 Jul 2012
Why "K>>"?
Run
>> dbquit all
to get out of debug mode!
Are plots ok?
per isakson
on 5 Jul 2012
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
on 5 Jul 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.
per isakson
on 5 Jul 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.
Soni huu
on 5 Jul 2012
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)
per isakson
on 5 Jul 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
on 6 Jul 2012
Edited: per isakson
on 6 Jul 2012
Does 11-02-2011.dat contain data from November or February?
Soni huu
on 6 Jul 2012
Edited: Soni huu
on 6 Jul 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
More Answers (2)
per isakson
on 6 Jul 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 Comments
Soni huu
on 6 Jul 2012
Edited: Soni huu
on 6 Jul 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
on 6 Jul 2012
Search "Code Cells" in the help to see if that is in your Matlab.
per isakson
on 6 Jul 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
on 6 Jul 2012
>> 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".
per isakson
on 6 Jul 2012
What is this: "rain_data(001).DailyRain"?
Did you miss to include the line
rain_data = struct([]);
Soni huu
on 6 Jul 2012
Edited: Soni huu
on 6 Jul 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".
per isakson
on 6 Jul 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.
Soni huu
on 6 Jul 2012
Edited: Soni huu
on 6 Jul 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".
Soni huu
on 6 Jul 2012
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
per isakson
on 6 Jul 2012
That explains
>> rain_data(1).DailyRain
??? Undefined variable "rain_data" or class "rain_data".
per isakson
on 6 Jul 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 Comments
per isakson
on 6 Jul 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.
See Also
Categories
Find more on MATLAB Mobile Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)