Downloading data from txt file
Afficher commentaires plus anciens
I was trying to download data from txt file using the importdata function however it downloads it into a structure with cell arrays in it. this is the code I am using. I need this data in a matrix of doubles if possible but it won't work.
delimiterIn = ' ';
[DataStructure] = importdata('iss.txt',delimiterIn);
DataCells = DataStructure(1).textdata(:,:);
This is a sample of what the txt file has inside:
1 25544U 98067A 16001.21631944 .00005382 00000-0 85171-4 0 9992
2 25544 51.6436 175.7680 0008248 349.1433 350.2600 15.55191287978846
1 25544U 98067A 16001.28471065 .00005383 00000-0 85187-4 0 9993
2 25544 51.6436 175.4264 0008246 349.3894 13.1693 15.55192096978860
1 25544U 98067A 16001.49479696 .00005567 00000-0 87829-4 0 9993
2 25544 51.6434 174.3772 0008262 350.0277 109.5268 15.55195766978890
1 25544U 98067A 16001.49479696 .00005567 00000-0 87829-4 0 9993
2 25544 51.6434 174.3772 0008262 350.0277 109.5268 15.55195766978890
.
.
.
Réponses (1)
Here is one way:
data = textscan( fileread( 'data.txt' ), '%s' ) ;
data = reshape( str2double( data{1} ), 17, [] ).' ;
data = data(:,~isnan( data(1,:))) ;
Then it is possible to split into two blocks if you prefer, for rows "1" and rows "2".
PS: it would be possible to extract non-numeric data as well if you needed.
6 commentaires
Jesus Christian
le 25 Oct 2017
There is a discrepancy that is not present in the slice that you gave above. If it is not too confidential you can send me the file at matlab@elitemail.org and I can have a look tomorrow. Otherwise I can think of a way to investigate (tomorrow).
In the meantime, do you get the same error if you replace the first line of code by
data = strsplit(strtrim(fileread('data.txt'))) ;
There is a discrepancy after line 1500. I copied lines 1499 to 1502 here:
1 25544U 98067A 16136.69611507 .00005252 00000-0 85076-4 0 9996
2 25544 51.6433 220.1152 0002135 107.8767 350.8535 15.54602232999921
1 25544U 98067A 16137.21209581 .00005359 00000-0 86643-4 0 9999
2 25544 51.6435 217.5406 0002111 109.7961 358.5795 15.54608822 13
You can see that lines 1499 and 1500 follow the 9-8 columns pattern present from the beginning of the file, but that lines 1501 and 1502 are 9-9. In fact, from line 1502 on, you loose regularity. The plot below shows the # of elements per line:

You can see that it is regular (dense) 8-9 until 1502. It seems that there are other, minor discrepancies in your data set. Sometimes there is a + in front of positive numbers, sometimes not. Sometimes there is a minus (do you really have negative values?). Also, looking at values between the 8 and 9 values versions of lines with ID 2, it seems that this is a filling problem:
2 25544 51.6433 220.9943 0002124 108.0375 83.7509 15.54599868999892
2 25544 51.6433 220.9943 0002124 108.0375 83.7509 15.54599868999892
2 25544 51.6433 220.1152 0002135 107.8767 350.8535 15.54602232999921
2 25544 51.6435 217.5406 0002111 109.7961 358.5795 15.54608822 13
2 25544 51.6440 215.8562 0001995 108.3968 92.4187 15.54614828 61
2 25544 51.6440 215.8562 0001995 108.3968 92.4187 15.54614828 61
You'll have to understand what is going wrong data-wise, and then we can find a way to process/import the content.
Here is how I looked up for the discrepancy by the way:
>> rows = strsplit( strtrim( fileread( 'iss.txt' )), '\n' ) ;
>> rows = cellfun( @strsplit, rows, 'Unif', false ) ;
>> rowSz = cellfun( 'length', rows ) ;
>> plot( rowSz ) ;
>> pos = strfind( rowSz, [9,9] ) ; % Look for first break of [9,8] pattern.
>> pos(1)
ans =
1501
Jesus Christian
le 26 Oct 2017
Walter Roberson
le 26 Oct 2017
Looks to me as if possibly some of the fields are fixed width -- that perhaps none of the lines are only 8 fields and instead the fields are running together. If so, then that second last field can be handled with %.12f
Cedric
le 26 Oct 2017
You have at least to understand the structure of the data that you are reading, and tell us if, as Walter suggests, there are 9 columns everywhere with fixed width or if it is something else. That must be documented somewhere, and especially the length of the last field.
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!