试图访问L1(1,4);索引越界因为大小(L1)= [0,0]?

时间:2013-04-18 23:45:31

标签: matlab file parsing gps

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

试图访问L1(1,4);索引越界,因为大小(L1)= [0,0]。

这是我用作源的文本文件的示例(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);

1 个答案:

答案 0 :(得分: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