how to accumulation a cell (x) data. from 30 file data.

i have 1440x9 cell data from a file, n i have 360 file data in a folder. how to accumulation data cell 3, from 30 file data.
ps:
1 format file is : mm-day-year.dat
2 folder name ex: 2011
3 data accumulation is from 01-01-2011 to 1-30-2011 (a month from 12 month)
4 how to plot result data poin 3 with month. in a year
thanks

 Accepted Answer

This is a start
function cssm( folder_spec, file )
03:05:00 .000 065.501 01** 4864 0086 0074 +19 03:06:00 .000 065.501 01** 4862 0095 0074 +19 03:07:00 R- .026 065.501 01** 4862 0105 0074 +19 03:08:00 R- .180 065.504 01** 4864 0105 0074 +19
sad = dir( fullfile( folder_spec, file ) );
for sa = transpose( sad )
fid = fopen( fullfile( folder_spec, sa.name ), 'r' );
cac = textscan( fid, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
fclose( fid );
end
end
Try:
  • set break point at first line
  • run
cssm( 'c:\your_path\2011\', '*.dat' )
  • experiment in break mode with the actual data
  • add working lines of code to the function
  • save and restart function when it becomes messy

43 Comments

this the result
cssm('c:\matlab7\work\org\2011\', '*.dat' )
??? Error using ==> cssm
Too many input arguments.
ps:
just 2 file .dat in 2011 folder
Have you edited cssm.m according to my answer? Do you have more than one cssm.m? Try "which cssm.m -all". You might want to change the name of the file.
.m =
function cac = cssm
03:05:00 .000 065.501 01** 4864 0086 0074 +19 03:06:00 .000 065.501 01** 4862 0095 0074 +19 03:07:00 R- .026 065.501 01** 4862 0105 0074 +19 03:08:00 R- .180 065.504 01** 4864 0105 0074 +19
fid = fopen( '09-25-2010.dat', 'r' );
cac = textscan( fid, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
fclose( fid );
end
cssmsum.m=
function cssmsum( folder_spec, file )
03:05:00 .000 065.501 01** 4864 0086 0074 +19 03:06:00 .000 065.501 01** 4862 0095 0074 +19 03:07:00 R- .026 065.501 01** 4862 0105 0074 +19 03:08:00 R- .180 065.504 01** 4864 0105 0074 +19
sad = dir( fullfile( folder_spec, file ) );
for sa = transpose( sad )
fid = fopen( fullfile( folder_spec, sa.name ), 'r' );
cac = textscan( fid, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
fclose( fid );
end
end
n i write:
cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
> clear >> cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
>> cac=cssmsum;
??? Error using ==> cssmsum
Too many output arguments.
Name of file and function must be the same!
And do not forget the important part about setting the breakpoint before running the program.
i fix it..
cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
>> cac=cssmsum;
??? Error using ==> cssmsum
Too many output argu
There is no output in the signature! If you want one output you have to change the signature to
function cac = cssmsum( folder_spec, file )
Pay more attention to the error messages. Mostly they are meaningful!
Doug Hull has made many useful demos on coding with Matlab. Watch a demo on using the debugger and search for more. Did you read the page in the documentation that Walter recommended?
just 2 file in 2011 folder
> clear
>> cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
ans =
Columns 1 through 6
[1440x8 char] [1440x2 char] [1440x1 double] [1440x1 double] [1440x4 char] [1440x1 uint32]
Columns 7 through 9
[1440x1 uint32] [1440x1 uint32] [1440x1 uint32]
>> cac=cssmsum;
??? Input argument "folder_spec" is undefined.
Error in ==> cssmsum at 8
sad = dir( fullfile( folder_spec, file ) );
>> cac
??? Undefined function or variable 'cac'.
If you got as far as that message about too many output arguments, then you did not put in the breakpoint as instructed!
walter : just 2 file in folder or 2880 line data.. that to much??
what is "folder_spec"?
cac = cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
2880 lines is trivial for these routines.
folder_spec is the name of a variable, the value of which in your case is 'C:\matlab7\work\org\2011\'. file is the name of another variable with the value, '*.dat'. The two serve as input arguments to the function cssmsum.
It is easier to use a small data file when you develop the code. 1440 line makes it awkward. However, 1440 is no problem for Matlab
cssmsum.m = function cac = cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
03:05:00 .000 065.501 01** 4864 0086 0074 +19 03:06:00 .000 065.501 01** 4862 0095 0074 +19 03:07:00 R- .026 065.501 01** 4862 0105 0074 +19 03:08:00 R- .180 065.504 01** 4864 0105 0074 +19
sad = dir( fullfile( 'C:\matlab7\work\org\2011\', '*.dat') );
for sa = transpose( sad )
fid = fopen( 'C:\matlab7\work\org\2011\', sa.name ), 'r' );
cac = textscan( fid, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
fclose( fid );
end
end
i write
clear
>> cssmsum( 'C:\matlab7\work\org\2011\', '*.dat' )
??? Error: File: C:\matlab7\work\org\cssmsum.m Line: 1 Column: 27 Missing MATLAB operato
Why did you change the signature? It shall read
function cssmsum( folder_spec, file )
or if you prefer
function cac = cssmsum( folder_spec, file )
Make sure your call matches the signature.
Why did you remove "fullfile" from the fopen command?
It is time
  • you read about functions in the Getting Started.
  • make some simple exercises with input and output arguments.
Did you watch the demos of Doug?
sometime the file have error data (C= calibration).. that take effect
08:48:00 .000 057.250 01** 4852 0058 0058 +24
09:31:00 C
09:32:00 ---- ---.--- 01** 0000 0000 0000 +00
09:33:00 ---- ---.--- 01** 0000 0000 0000 +00
09:34:00 ---- ---.--- 01** 0000 0000 0000 +00
09:35:00 .000 000.000 01** 4856 0058 0300 +24
09:36:00 .000 000.000 01** 4852 0058 0300 +25
09:37:00 .000 000.000 01** 4854 0058 0300 +25
When your lines are not in a consistent format, do not use textscan(). Instead, fgetl() the line, figure out which of the formats the line is in, sscanf() with the appropriate format (or discard the line), append the values to the list of values you are building up, and loop back for the next line. Remember to test that the value returned by fgetl() ischar() as fgetl() will return -1 (a numeric value) at end of file.
The function, textscan, is made to handle text files, which contain
  • header lines at the top of the file
  • data lines, all of which have the same format, i.e. number and type of columns
  • comment lines, which are ignored
All these type of lines are optional.
Thus, your file cannot be read and parsed by textscan in one step.
You save yourself trouble if you gather information on the file before you start to make code. Did you read the manual of the instrument in question?
Are there more special cases than "Calibration"?
How shall these special cases be handled?
I have read the manual. the special cases just calibration. i want the special case (calibration) become 0 (---- to 00). but if posible, i want delete the line of error case..
What happend to "R-"?
What about the line "09:31:00 C"?
Does "---" differ from "----"?
just delete "09:31:00 C"? and usualy 3 line after this line will error. and i want to delete
" " is no rain
"R-" is low rai = 0 to 2.5 (column 3)
"R " is medium rain 2.5 to 7.5
"R+" is hevy rain 7.5 to ∞
so we dont neeed colum 2 coz we can read from column 3
sorry : at all times 3 line after this line will error
Does "---" differ from "----"? what do u mind? 7.5 to ∞ ?
error data just have 2 cell in first line and 8 cell after 3 line. normal data have 9 cell
Markup the data lines as "Code". (Below, I have intended the data lines by four spaces.) That will make communication more effective.
The format string I proposed above parses lines like the two below
03:07:00 R- .026 065.501 01** 4862 0105 0074 +19
03:08:00 R- .180 065.504 01** 4864 0105 0074 +19
However, that format string will not parse lines like the following three (they are four positions shorter)
09:35:00 .000 000.000 01** 4856 0058 0300 +24
09:36:00 .000 000.000 01** 4852 0058 0300 +25
09:37:00 .000 000.000 01** 4854 0058 0300 +25
That is because there is nothing in these lines that matches "%2c".
Prepare a sample file that includes all types of data lines that may appear in a file.
09:35:00 .000 000.000 01** 4856 0058 0300 +24
09:36:00 .000 000.000 01** 4852 0058 0300 +25
in cell 2 have 4 space, 2 space for blank data
09:37:00," "," "," ",.000 000.000 01** 4854 0058 0300 +25
Sounds like a lot of trouble to go through to me. I would use a much more appropriate tool to filter the data before even reading it into MATLAB. For example, in Unix-like systems, if the data was in rain.txt then
sed -e '/C$/,+3d' rain.txt > newrain.txt
This can also be rewritten in terms of perl (a programming language which is installed with every MATLAB installation)
walter r... :'(
I will do everything you command..
but i'll take time coz my englis is bad..
This file reads your sample file. First it reads the whole files to a cell array, cac, with one line of the file per row of the array. Next it removes rows, which match "16:45:00 C" or "16:46:00 ----". Lastly it parse the data. It is tested. Try
>> cac = ReadSoniData( 'h:\m\cssm', 'SoniData.txt' );
.
function cac = ReadSoniData( folder_spec, file )
sad = dir( fullfile( folder_spec, file ) );
for sa = transpose( sad )
fid = fopen( fullfile( folder_spec, sa.name ), 'r' );
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 = textscan( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
end
end
Your file at 4Share didn't cause problems to ReadSoniData.
plot(cac{3})
shows a peak at index just below 1000.
??h:\m\cssm' : cssm is folder or cssm.m?
i save file data as SoniData.dat at C:\matlab7\work\org
i save ur scrip in m file with name (sonid.m) at C:\matlab7\work\org
the result is:
>> cac = ReadSoniData( 'C:\matlab7\work\org', 'SoniData.dat' );
??? Undefined command/function 'ReadSoniData'.
>> sonid
K>>
K>>
what mind
>> sonid
??? Input argument "folder_spec" is undefined.
Error in ==> sonid at 4
sad = dir( fullfile( folder_spec, file ) );
its oke if my file with .dat format??
You have to copy the code, which I recently provided in a comment above, to an m-file and save that as "SoniDataRead.m". And rename your sample data file to SoniData.dat. Do I need say that explicitely? Then try
>> cac = ReadSoniData( 'C:\matlab7\work\org', 'SoniData.dat' );
or you may call it "sonid", but you have to pass the input arguments, i.e
>> cac = sonid( 'C:\matlab7\work\org', 'SoniData.dat' );
"h:\m\cssm" is the name of a folder. "cssm.m" is the name of an m-file.
i work with matlab 7 14.. its oke???
cac = ReadSoniData( 'C:\matlab7\work\org', 'SoniData.dat' );
>> cac = ReadSoniData( 'C:\matlab7\work\org', 'SoniData.dat' );
??? Undefined command/function 'ReadSoniData'.
>> plot(cac{3})
??? Undefined variable "cac" or class "cac".
Try
>> which ReadSoniData -all
>> which sonid -all
8c is not doble?
>> cac = sonid( 'C:\matlab7\work\org', 'SoniData.dat' );
??? Error using ==> textscan
First input must be of type double.
Error in ==> sonid at 16
cac = textscan( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
K>> which sonid -all
C:\matlab7\work\org\sonid.m
K>> which ReadSoniData -all
'ReadSoniData' not found.
K>> which ReadSoniData -all
C:\matlab7\work\org\ReadSoniData.m
K>>
The problem is, I cannot know what code is inside sonid.m. I strongly doubt that it is the code I provided in the comment above a couple of hours ago. The error message says "at 16", but textscan( str, ..) in my code is at line 25.
I believe you use the wrong code.
Matlab documentations says "C = textscan(str, ...) reads data from string str."
Sorry, "is at line 25" applies to my version, which has some blank lines.
However, it works here.
can u ceck my m file please(1kb). or can u upload your m file, coz i just have 19 line code and u have 28 line code
I downloaded your file and renamed it to "ReadSoniData_2.m" and I changed the name of the function to "ReadSoniData_2". Then I run
>> clear all
>> cac = ReadSoniData_2( 'h:\m\cssm', '11-02-2011.dat' );
>> plot( cac{3})
It worked without problems. The value of the peak is 60 plus.

Sign in to comment.

More Answers (1)

Next step, try:
>> dbstop ReadSoniData 2
>> cac = ReadSoniData( 'h:\m\cssm', '11-02-2011.dat' );
However, if you didn't watch Doug's video you might need to do that now. Then step through the code.
.
The data file, '11-02-2011.dat', shall be in your folder, 'C:\matlab7\work\org'.
You could add an assert to the file after "sad = dir(..."
sad = dir( fullfile( folder_spec, file ) );
assert( not( isempty( sad ) ) ...
, 'ReadSoniData:NoFileFound' ...
, 'Cannot find files matching "%s"' ...
, fullfile( folder_spec, file ) )

9 Comments

i thing my problem is like this problem, please ceck sir, can u explain to me..
i use matlab 7 (R14)
"MATLAB prior to MATLAB 7.0.4" that explains that problem.
Replace the line
cac = textscan( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'Delimiter', ' ', 'Whitespace', '' );
by
cac = cell(1,9);
[cac{:}] = strread( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'delimiter', ' ', 'whitespace', '' );
Note the lower-case letters in 'delimiter' and 'whitespace'
If
[cac{:}]
causes problems replace it by
[cac{1},cac{2},cac{3},cac{4},cac{5},cac{6},cac{7},cac{8},cac{9}]
its work sir.. THANKS YOU VERY MUCH..
BE AWARE my first answer said "This is a start". The function we arrived at will only return the result of the last of the 30 files.
now how 30 file is work??
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.
Case closed!

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!