. I'm trying to find out some indices and process a file with rows (comented) with diferent numbers of columns. Any help?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dorfschafer
il 2 Apr 2015
Commentato: Dorfschafer
il 6 Apr 2015
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 Commenti
per isakson
il 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"   is only a step on the way to read and parse the file?
Does the entire file fit comfortable in memory?
Risposta accettata
per isakson
il 6 Apr 2015
- "the indices of these last ones"   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"   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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!