Read text data and create array?
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a .txt file with ISS orbital parameters that I'm looking to import into Matlab and use to plot things like satellite position, for example.
I have successfully pulled the data from the website and printed to the attached .txt file but when I use readtable(), Matlab generates a 212x1 matrix instead of splitting the columns and creating a full array. Unfortunately, the format from the website hosting the orbit data features an unequal number of delimiters as well.
Any help and/or guidance on how to read this data, split it into columns, and create variables from it would be much appreciated. I do not need the string data (i.e. "ISS (ZARYA) ") from the top of each two row data set. Thanks.
댓글 수: 6
dpb
2020년 3월 29일
WRT to the above comment and the table...how you want to name stuff? For example, there are multiple pieces of data buried in some of the variables like the Satellite Number and Classification are run together in one string but are separate fields--you want/need both of the composite?
Similar with the Designator excepting it has three composite pieces buried in it.
And, continues with the epoch year/day that are also run together.
Need to define what pieces you need as the individual variables or what you want to leave as encoded and deal with separating out the pieces on an as-needed basis.
You make a list of wanted variables and I'll see about encoding...since the guv now has us on lockdown, too, guess will not push the issue too hard.
채택된 답변
Ameer Hamza
2020년 3월 30일
편집: Ameer Hamza
2020년 3월 30일
Try this:
%% read data
f = fopen('issd.txt');
data = {};
while true
line = fgetl(f);
if line == -1
break;
end
data{end+1} = line;
end
fclose(f);
%% split data
data(2:2:end) = []; % delete empty lines
names = data(1:3:end);
line1 = cell2mat(data(2:3:end)');
line2 = cell2mat(data(3:3:end)');
%% Parse data
% helper functions
f = @(x) mat2cell(line1(:,x), ones(size(line1,1),1), numel(x), 1);
g = @(x) num2cell(str2num(line1(:,x)));
h = @(x,y) num2cell(str2num(strcat('.', line1(:,x))) .* 10.^str2num(line1(:,y)));
line1_split = [f(1) g(3:7) f(8) g(10:11) g(12:14) f(15:17) g(19:20) g(21:32) ...
g(34:43) h(46:50,51:52) h(55:59,60:61) g(63) g(65:68) g(69)];
g = @(x) num2cell(str2num(line2(:,x)));
line2_split = [f(1) g(3:7) g(9:16) g(18:25) g(27:33) g(35:42) g(44:51) ...
g(53:63) g(64:68) g(69)];
%% create table
T = cell2table([names' line1_split line2_split]);
T.Properties.VariableNames = {'Name', ...
'LN1', ...
'Satellite Number', ...
'Classification', ...
'Intl. Designator (launch year)', ...
'Intl. Designator (number of the year)', ...
'Intl. Designator (piece of the launch)', ...
'Epoch Year', ...
'Epoch Day', ...
'First Derivative (Mean Motion)', ...
'Second Derivative (Mean Motion)', ...
'Drag term', ...
'Ephemeris type', ...
'Element set number', ...
'Checksum1', ...
'LN2', ...
'Catalog number', ...
'Inclination', ...
'Right Ascension', ...
'Eccentricity', ...
'Argument of Perigee', ...
'Mean Anomaly', ...
'Mean Motion', ...
'Revolution number', ...
'Checksum2'};
댓글 수: 6
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

