MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Downloading data from txt file

Jesus Christian さんによって質問されました 2017 年 10 月 24 日
最新アクティビティ Cedric Wannaz
さんによって コメントされました 2017 年 10 月 26 日
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
.
.
.

  2 件のコメント

Cedric Wannaz
2017 年 10 月 24 日
Which data do you need? What about the ones with letters/dashes? Do you want to discard them, to extract them in a separate array, ..? They have 9 columns actually, whereas the others have 8.
Jesus Christian 2017 年 10 月 25 日
I need the entire bottom row of every set in one array of 8 elements (i.e. the rows with a 2 as first element)
I also need the top row (rows with 1 as a first element) but only the elements without letters
Basically, I need all the elements on each row that don't have letters.

サインイン to comment.

1 件の回答

Cedric Wannaz
回答者: Cedric Wannaz
2017 年 10 月 25 日
編集済み: Cedric Wannaz
2017 年 10 月 25 日

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 件のコメント

Jesus Christian 2017 年 10 月 26 日
there are discrepancies but i cannot edit the text file how would I get around this?
Walter Roberson
2017 年 10 月 26 日
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 Wannaz
2017 年 10 月 26 日
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.

サインイン to comment.



Translated by