. I'm trying to find out some indices and process a file with rows (comented) with diferent numbers of columns. Any help?

3 views (last 30 days)
I have a file with data and some comented rows (lat,lon and date). I'm trying to find out the indices of these last ones. Then, these rows have more columns (about 5) than the not comented rows (2). The matlab can't process this way. Need some help. Ex:
% 189° 25° 1980 4 27
3 28,9
4 28,7
5 27,0
6 25,6
[...]
  2 Comments
per isakson
per isakson on 2 Apr 2015
Does your file look something like this?
% 189° 25° 1980 4 27
3 28,9
4 28,7
5 27,0
6 25,6
...
% 189° 25° 1980 4 28
3 28,9
4 28,7
5 27,0
6 25,6
...
% 189° 25° 1980 4 29
3 28,9
4 28,7
5 27,0
6 25,6
...
comma, ",", as decimal separator? (Matlab has problems with that.)
"find out the indices" &nbsp is only a step on the way to read and parse the file?
Does the entire file fit comfortable in memory?
Dorfschafer
Dorfschafer on 6 Apr 2015
Hi Isakson, No, I expressed myself badly.. The file contains dots, not commas. Yes, it looks like your model and fitted well in memory (until then). I need to find out these indices because i'm only interested in certain lats and lons.. so, i need to know where they are and a way to read and parse these particular set of the file.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 6 Apr 2015
  • "the indices of these last ones" &nbsp I'm not sure what "indices" refers to.
  • "I need to find out these indices because i'm only interested in certain lats and lons" &nbsp If it doesn't cause problems with memory or performance I find it easier to read and parse the entire file in a separate step. Furthermore, that increases the chances that the code can be reused. The function cssm performs that first step.
>> sas = cssm()
sas =
1x3 struct array with fields:
sdn
Lat
Long
data
>> is25 = [sas.Lat] == 25;
>> datestr( [sas(is25).sdn], 'yyyy-mm-dd' )
ans =
1980-04-27
1980-04-28
where
function sas = cssm()
str = fileread('data.txt');
deg = char(176);
pos = '(%[^\n\r]+)'; % matches a substring starting with "%" ending at new-line
cac = regexp( str, [pos,'[\r\n ]+([\d\.\s ]+)'], 'tokens' );
len = length( cac );
sas(1,len) = struct( 'sdn', [], 'Lat',[], 'Long',[], 'data', [] );
for jj = 1 : len
buffer1 = sscanf( cac{jj}{1}, ['%*c %d',deg,'%d',deg,'%d%d%d'] );
buffer2 = textscan( cac{jj}{2}, '%f%f', 'CollectOutput', true );
sas(jj).Long = buffer1(1);
sas(jj).Lat = buffer1(2);
sas(jj).sdn = datenum( buffer1(3), buffer1(4), buffer1(5) );
sas(jj).data = buffer2{1};
end
end
and where data.txt contains
% 187° 25° 1980 4 27
3 28.9
4 28.7
5 27.0
6 25.6
% 188° 25° 1980 4 28
3 28.9
4 28.7
5 27.0
6 25.6
% 189° 30° 1980 4 29
3 28.9
4 28.7
5 27.0
6 25.6
  1 Comment
Dorfschafer
Dorfschafer on 6 Apr 2015
By 'indices', i meant, putting into a matrix, the numbers of the rows with lats,lon,etc.. would have. I need to reuse this code several times.
Thanks for answering Isakson, i ll try your code (it doesn't looks like so simple), then ll come back here.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!