How to using try and catch correctly to force loop go to next iteration?

So I know how the try and catch work, but it wont go to next iteration correctly.
I already know this line will casue problem becasue the input files,
[position, velocity] = states(sat, time, "CoordinateFrame", "ecef");
but there are too much so I dont have time to check each one
therefore I use try to hold the all main part of my loop
however after it got error and go into catch part, actually it will go to next iteration
it wont do the calculation for the next TLE file and output the [position, velocity] which I need
the value will stay in the first error one
how can I let it work correctly? thanks
%ISS NORAD ID (25544)
%2019/08/13
%2023/08/12
clc;
clear;
close all;
%% real time
% List of TLE file names
%tleFiles = ["satellite1.tle", "satellite2.tle", "satellite3.tle"];
for z = 1:7758
fileName = sprintf('longlong%d.tle',z); % Generate the file name
tleFiles{z} = fileName; % Read the file contents
end
% Define the time range for propagsation
startTime = datetime(2019,8,13);
stopTime = datetime(2023,8,12);
sampleTime =86400;
timeRange = startTime:seconds(16261):stopTime;
% Create the satellite scenario
sc = satelliteScenario(startTime, stopTime, sampleTime);
% Loop through each TLE file
for i = 1:7758
try
tleFile = tleFiles{i};
tleFilePath = tleFile;
[epochYearArray, month, day] = extractTLEData(tleFilePath);
% Create the satellite object using the TLE file
sat = satellite(sc, tleFile);
% Calculate the year for the specified date
year = 2000 + epochYearArray;
% Specify the time for which you want to get position and velocity
time = datetime(year, month, day, 12, 0, 0);
% Compute position and velocity in ECEF coordinate frame
[position, velocity] = states(sat, time, "CoordinateFrame", "ecef");
% Display the results
disp("TLE File: " + tleFile);
position_realn(i) = (norm(position') / 1000) - 6371;
velocity_realn(i) = norm(velocity') / 1000;
disp(time);
disp("Position (ECEF) [km]: " + position_realn(i));
disp("Velocity (ECEF) [km/s]: " + velocity_realn(i));
disp("-------------------------------");
catch err
disp("Error for TLE file " + tleFile + ": " + err.message);
end
end
%play(sc)
%% SGP4
startTime = datetime(2019,8,13);
stopTime = datetime(2023,8,12);
sampleTime =86400;
sc1 = satelliteScenario(startTime,stopTime,sampleTime);
tleFile = "long1.tle";
satSGP4 = satellite(sc1,tleFile, ...
"Name","satSGP4", ...
"OrbitPropagator","sgp4");
satSDP4 = satellite(sc1,tleFile, ...
"Name","satSDP4", ...
"OrbitPropagator","sdp4");
[positionSGP4,velocitySGP4] = states(satSGP4,"CoordinateFrame", "ecef");
[positionSDP4,velocitySDP4] = states(satSDP4,"CoordinateFrame", "ecef");
for x=1:7758
positionSGP4_n(x) = (norm(positionSGP4(:,x)')/1000)-6371;
velocitySGP4_n(x) = norm(velocitySGP4(:,x)')/1000;
positionSDP4_n(x) = (norm(positionSDP4(:,x)')/1000)-6371;
velocitySDP4_n(x) = norm(velocitySDP4(:,x)')/1000;
end
%v = satelliteScenarioViewer(sc1);
%play(sc1)
%% RelativePosition
re_pos_sgp4=abs(position_realn-positionSGP4_n);
re_pos_sgp4_mean = mean(re_pos_sgp4);
re_pos_sdp4=abs(position_realn-positionSDP4_n);
re_pos_sdp4_mean = mean(re_pos_sdp4);
test_r_p=(positionSDP4_n-positionSGP4_n);
figure;
subplot(2,1,1)
plot(timeRange,re_pos_sgp4,'r-',timeRange,re_pos_sgp4_mean,'b->')
xlim("auto");
ylim("auto");
ylabel('km');
title('RelativePosition:SGP4');
subplot(2,1,2)
plot(timeRange,re_pos_sdp4,'r-',timeRange,re_pos_sdp4_mean,'b->')
xlim("auto")
ylim("auto");
ylabel('km');
title('RelativePosition:SDP4');

Answers (1)

Your try/catch works just fine, but your files are failed in the row. But as you are too lazy to check it is true or not it is just a speculation.
One experiment you could do is loop backward
for i = 7758:-1:1
try
...
catch
...
end
end

Products

Release

R2023a

Asked:

on 14 Aug 2023

Edited:

on 14 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!