![ea4073c5b9ab5e73a149edc44e849a4d.png](https://i-blog.csdnimg.cn/blog_migrate/b05669d3c1cb4f306edc1b7ccf8e6840.jpeg)
在上一篇“基于STK/Matlab的Starlink星座仿真分析”(https://zhuanlan.zhihu.com/p/68385977)中,我们介绍了使用Matlab调用STK进行仿真分析的原理,这里进一步给出两个示例,主要使用STK Objects Library,若想撇除STK的界面,可以换成STK X Library。
示例1:将指定双行根数转换为J2000根数
% 本函数实现双行根数转换为J2000开普勒根数的功能
% 戴正旭 2019-12-14
function transTLE
% 读入的3TLE文件目标和输出结果目录
str3TLEFilePath = 'F:TLE3le20191027_part.txt';
strOutFilePath = 'F:TLE3le20191027_J2000.txt';
% 指定转换的卫星NORAD编号
matNoradID = [41765];
% 指定创建STK场景名称
strScenarioName = 'TranslateTLE';
% 指定创建STK场景时间
strBegTime = '9 Dec 2019 23:00:00.000';
strEndTime = '10 Dec 2019 12:00:00.000';
% 处理编号
nStarCount = size(matNoradID,2);
% 读入所有3TLE根数
fidin = fopen(str3TLEFilePath);
nReadNum = 0;% 读文件计数
vecTLE = [];
matReadNum = [];
while ~feof(fidin) && nStarCount>nReadNum
% 读取根数
strNameLine = fgetl(fidin);
% 避免空格
if isempty(strNameLine)
continue;
end
% 处理名字
strNameLine = regexp(strNameLine,' ','split');
strName = strNameLine(2);
strTLELine1 = fgetl(fidin);
strTLELine2 = fgetl(fidin);
% 获取NoradID
vecTLELine2 = regexp(strTLELine2,' ','split');
vecTLELine2(strcmp(vecTLELine2,''))=[];
if find(matNoradID==str2double(vecTLELine2(2)))
% 增加计数
nReadNum = nReadNum+1;
tmpTLE.Name = [char(strName) '_' char(vecTLELine2(2))];
tmpTLE.Code = str2double(vecTLELine2(2));
tmpTLE.Line1 = strTLELine1;
tmpTLE.Line2 = strTLELine2;
vecTLE = [vecTLE,tmpTLE];
matReadNum = [matReadNum,str2double(vecTLELine2(2))];
end
end
fprintf('指定卫星数:%d,读入卫星数:%dn',nStarCount,nReadNum);
disp('未找到卫星列表:');
disp(setdiff(matNoradID,matReadNum));
fclose(fidin);
%% 创建STK场景
disp('打开STK程序,创建一个场景');
try
% 获取运行中的STK实例句柄
uiapp = actxGetRunningServer('STK11.application');
root = uiapp.Personality2;
checkempty = root.Children.Count;
if checkempty == 0
% 如果未发现场景,新建一个
uiapp.visible = 1;
root.NewScenario(strScenarioName);
scenario = root.CurrentScenario;
else
% 如果发现打开着的场景,询问是否关闭重建
rtn = questdlg({'Close the current scenario?',' ','(WARNING: If you have not saved your progress will be lost)'});
if ~strcmp(rtn,'Yes')
return
else
root.CurrentScenario.Unload
uiapp.visible = 1;
root.NewScenario(strScenarioName);
scenario = root.CurrentScenar