% Parse NORAD Two-Line Element sets and determine year in which the ISS was
% launched.

fid = fopen('norad.txt', 'r');

% Read name of first entry
name = fgetl(fid);

% Name field contains trailing spaces, so only compare prefix
target = 'ISS';
while ~feof(fid) && ~strcmp(name(1:length(target)), target)
% Skip next two lines
fgetl(fid);
fgetl(fid);
% Read name of next entry
name = fgetl(fid);
% Note: Reading the next line at the end of a while-loop in MATLAB is
% normally an "anti-pattern", because feof will return true if the last
% line was just read. It is okay here because we know that lines are
% in groups of three.
end

% Only keep parsing if we exited the loop due to finding the desired name
% (and not because we reached the end of the file)
if ~feof(fid)
% Read first line of record and extract year
line1 = fgetl(fid);
yr = str2double(line1(10:11)) + 1900;
% Resolve Y2k issue by leveraging the fact that no satellites were
% launched before 1957
if yr < 1957
yr = yr + 100;
end
fprintf('%s was launched in %d\n', deblank(name), yr)
else
fprintf('%s not found\n', target);
end

fclose(fid);