Read text data and create array?

3 visualizzazioni (ultimi 30 giorni)
David Hamblin
David Hamblin il 28 Mar 2020
Commentato: Ameer Hamza il 31 Mar 2020
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 Commenti
dpb
dpb il 29 Mar 2020
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.
David Hamblin
David Hamblin il 30 Mar 2020
I actually followed your advice above and turns out someone did do something just like this.
Matlab linked to their code at GitHub here.

Accedi per commentare.

Risposta accettata

Ameer Hamza
Ameer Hamza il 30 Mar 2020
Modificato: Ameer Hamza il 30 Mar 2020
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 Commenti
David Hamblin
David Hamblin il 30 Mar 2020
This works very well. Thanks much for your help!
Ameer Hamza
Ameer Hamza il 31 Mar 2020
Glad to be of help.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion 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!

Translated by