1

我正在运行这段代码并试图解析它,但它给了我这个错误:

尝试访问 L1(1,4);索引超出范围,因为 size(L1)=[0,0]。

这是我用作源的文本文件的示例(tle 2009),您可以将其复制并粘贴为文本文件,然后运行下面的代码。当我绝望时,我会感谢你的帮助。太感谢了

1 32790U 08021H   09001.16229234  .00000278  00000-0  42282-4 0  2397
2 32790 097.9691 066.3073 0014889 210.0082 150.0281 14.81422378 36693
1 32790U 08021H   09002.17545217 -.00000097  00000-0 -54482-5 0  2400
2 32790 097.9693 067.3149 0015060 206.2003 153.8437 14.81421045 36843
1 32790U 08021H   09003.18861152 -.00000056  00000-0 -30988-6 0  2413
2 32790 097.9691 068.3223 0015301 202.5601 157.4939 14.81421032 36995
1 32790U 08021H   09004.20176971  .00000327  00000-0  48542-4 0  2428
2 32790 097.9692 069.3304 0015360 199.1526 160.9109 14.81422711 37140
1 32790U 08021H   09005.21492806  .00000366  00000-0  53567-4 0  2435
2 32790 097.9688 070.3376 0015282 196.0531 164.0195 14.81423660 37291
1 32790U 08021H   09006.43071783  .00000265  00000-0  40677-4 0  2444

-

%%
clc;
clear all;
mu = 398600;             % Standard gravitational parameter for the earth
fname = 'tle 2009.txt';      % TLE file name 
% Open the TLE file and read TLE elements
fprintf('a[km]    e    inc[deg]    RAAN[deg]  w[deg]   M[deg]  T[min]  h_p[km]    h_a[km]\n\n ')
fid = fopen(fname, 'rb');
i = 1;
OE2=[];
while ~feof(fid)
L1 = fscanf(fid,'%d%6d%*c%5d%*c%f%f%f%5d%*c%*d%5d%*c%*d%d%4d',[1,9]);
L2 = fscanf(fid,'%d%6d%f%f%f%f%f%11f',[1,9]);

%epoch = L1(1,4)*24*3600;        % Epoch Date and Julian Date Fraction
Db    = L1(1,5);                % Ballistic Coefficient
inc(i)= L2(1,3);                % Inclination [deg]
RAAN(i) = L2(1,4);              % Right Ascension of the Ascending Node [deg]
e(i)  = L2(1,5)/1e7;            % Eccentricity 
w(i)  = L2(1,6);                % Argument of periapsis [deg]
M(i)  = L2(1,7);                % Mean anomaly [deg]
n     = L2(1,8);                % Mean motion [Revs per day]
% Orbital parametres
a(i) = (mu/(n*2*pi/(24*3600))^2)^(1/3);       % Semi-major axis [km]    
T(i) = 2*pi*sqrt(a(i)^3/mu)/60;               % Period in [min]
Re = 6371; 
h_p(i) = (1 - e(i))*a(i) - Re;                % Perigee Altitude [km]
h_a(i) = (1 + e(i))*a(i) - Re;                % Apogee Altitude [km]
% Orbital Elements  
OE = [a(i) e(i) inc(i) RAAN(i) w(i) M(i) T(i) h_p(i) h_a(i)];
OE2 = [OE2;a(i) e(i) inc(i) RAAN(i) w(i) M(i) T(i) h_p(i) h_a(i)];
fprintf('%4.2f  %7.7f   %4.4f  %4.4f   %4.4f  %4.4f  %4.2f  %4.2f  %4.2f \n', OE);
i = i+1;
end
%fclose(fid);
4

1 回答 1

2

问题是您的文本文件中有奇数行。通过这个 while 循环,您正在阅读 2 行。因此,当您在最后一行时,L1 已填充,但 L2 未填充。

您必须更改循环以在偶数行处停止,或者在像这样处理之前创建一个条件

while ~feof(fid)
L1 = fscanf(fid,'%d%6d%*c%5d%*c%f%f%f%5d%*c%*d%5d%*c%*d%d%4d',[1,9]);
L2 = fscanf(fid,'%d%6d%f%f%f%f%f%11f',[1,9]);

if(L2)    
     %epoch = L1(1,4)*24*3600;        % Epoch Date and Julian Date Fraction
     Db    = L1(1,5);                % Ballistic Coefficient
     ...
end
于 2013-04-19T00:15:54.430 回答