Using Java API from MATLAB to Start up and Control STK Tutorial
The following tutorial demonstrates how to use Java API from within MATLAB to start up and control STK.
This tutorial is written for Windows, but it can be used for Linux with the only differences being that the paths are slightly different and some desktop-specific UI jar files are not available.
Configure MATLAB for STK
To configure MATLAB for STK, perform the following two procedures.
Configure MATLAB to Find the STK Native jni Libraries
You need to do the following procedure just once to configure MATLAB to find the STK native jni libraries.
- Write the following command in the MATLAB editor:
- If MATLAB prompts you to create a new file, click Yes. A file similar to the following is created.
- Add one line:
- Save and close the file, restart MATLAB.
cd(prefdir)
edit javalibrarypath.txt
C:\Users\<user name>\AppData\Roaming\MathWorks\MATLAB\R2019b\javalibrarypath.txt
<Install_Dir>\bin\
Configure MATLAB to Find the stk jar Files
You need to do the following procedure just once to configure MATLAB to find the stk jar files.
You can either add the path to each jar file in a script each time after starting up MATLAB or add them all to a text file just once. If you prefer to have the code do it each time before using STK in MATLAB, use the javaaddpath MATLAB command for each jar file. For example:
stkInstallPath = '<Install_Dir>\bin\'
javaaddpath([stkInstallPath , 'agi.core.jar']
Repeat for all other jar files. If you prefer to load all files into a file just once, follow these steps:
- Write the command in the MATLAB editor:
- If MATLAB prompts you to create a new file, click Yes. A file similar to the following is created.
- Add all jar files that you need for your project to javaclasspath.txt. If you are not sure which ones you need, copy the block below which contains all jar files located in <Install_Dir>\bin:
- Save and close the file, restart MATLAB.
edit javaclasspath.txt
C:\Users\<user name>\AppData\Roaming\MathWorks\MATLAB\R2019b\javaclasspath.txt
<Install_Dir>\bin\agi.core.jar
<Install_Dir>\bin\agi.core.swing.jar
<Install_Dir>\bin\agi.core.swt.jar
<Install_Dir>\bin\agi.stk.attr.jar
<Install_Dir>\bin\agi.stk.core.jar
<Install_Dir>\bin\agi.stk.core.swing.jar
<Install_Dir>\bin\agi.stk.javadocs.jar
<Install_Dir>\bin\agi.stk.plugin.accessconstraints.jar
<Install_Dir>\bin\agi.stk.plugin.astrogator.plugin.jar
<Install_Dir>\bin\agi.stk.plugin.crdn.jar
<Install_Dir>\bin\agi.stk.plugin.graphics.jar
<Install_Dir>\bin\agi.stk.plugin.jar
<Install_Dir>\bin\agi.stk.plugin.stk.jar
<Install_Dir>\bin\agi.stk.plugin.util.jar
<Install_Dir>\bin\agi.ui.application.jar
<Install_Dir>\bin\agi.ui.core.jar
<Install_Dir>\bin\agi.stk.ui.jar
<Install_Dir>\bin\agi.stkengine.jar
<Install_Dir>\bin\agi.stkgraphics.jar
<Install_Dir>\bin\agi.stkobjects.astrogator.jar
<Install_Dir>\bin\agi.stkobjects.aviator.jar
<Install_Dir>\bin\agi.stkobjects.jar
<Install_Dir>\bin\agi.stkutil.jar
<Install_Dir>\bin\agi.stkvgt.jar
<Install_Dir>\bin\agi.stkx.initialization.jar
<Install_Dir>\bin\agi.stkx.jar
<Install_Dir>\bin\agi.stkx.swing.jar
<Install_Dir>\bin\agi.stkx.swt.jar
<Install_Dir>\bin\agi.swing.jar
<Install_Dir>\bin\agi.swt.jar
At this point, MATLAB is configured and can find the STK libraries.
Initialize the STK Java API
Initializing the STK Java API varies depending on how MATLAB and STK interact:
- automation. Automate STK Desktop running as a separate process (Windows only).
- custom application. Embed STK Engine running inside the MATLAB process (Windows and Linux).
The code below specifies the namespaces for each type for documentation purposes. An alternative is to use imports ([import agi.core.awt;] for instance).
Initialization for automation
Pick one of the three ways to run STK:
- Start a new instance of STK.
- Attach to an already running instance of STK.
- Start an STK/Engine application.
Each method requires the following, and then the method-specific code to get the root object.
agi.core.awt.AgAwt_JNI.initialize_AwtDelegate(); agi.core.awt.AgAwt_JNI.initialize_AwtComponents(); agi.stk.ui.AgStkAutomation_JNI.initialize(true);
OPTION 1: To start a new instance of STK Desktop
stkDesktop = agi.stk.ui.AgStkUi(); stkDesktop.setVisible(true); root = stkDesktop.getPersonality2();
OPTION 2: To attach to already running STK Desktop
stkDesktop = agi.stk.ui.AgStkUi.getStkUiInstance(); root = stkDesktop.getPersonality2();
OPTION 3: initialization for STK Engine
agi.core.awt.AgAwt_JNI.initialize_AwtDelegate(); agi.core.awt.AgAwt_JNI.initialize_AwtComponents(); agi.stkengine.AgStkCustomApplication_JNI.initialize(true); stkEngine = agi.stkx.AgSTKXApplicationClass();
For better performance, turn off graphics if not needed:
stkEngine.setNoGraphics(true); root = agi.stkobjects.AgStkObjectRootClass();
Code Snippet Example
The following code snippet shows how to use the API:
root.newScenario('MatlabScenario'); root.getCurrentScenario().setTimePeriod('1 Sep 2020 12:00:00.00', '31 Sep 2020 12:00:00.00'); root.rewind; javaMethod('_new', root.getCurrentScenario().getChildren(), agi.stkobjects.AgESTKObjectType.E_SATELLITE, 'Seed'); root.executeCommand('OrbitWizard */Satellite/Seed Circular Inclination 98 Altitude 400 RAAN 0'); root.executeCommand('Walker */Satellite/Seed Type Delta NumPlanes 2 NumSatsPerPlane 4 InterPlanePhaseIncrement 0'); root.executeCommand('ImportFromDB * City "C:\\ProgramData\\AGI\\STK_ODTK 13\\Databases\\City\\stkCityDb.cd" Class Facility CityName Philadelphia Province Pennsylvania Country USA Type "Populated Place"'); root.executeCommand('ImportFromDB * City "C:\\ProgramData\\AGI\\STK_ODTK 13\\Databases\\City\\stkCityDb.cd" Class Facility CityName Pittsburgh Province Pennsylvania Country USA Type "Populated Place"'); root.executeCommand('ImportFromDB * City "C:\\ProgramData\\AGI\\STK_ODTK 13\\Databases\\City\\stkCityDb.cd" Class Facility CityName Seattle Province Washington Country USA Type "Populated Place"'); root.getUnitPreferences().setCurrentUnit('DateFormat', 'UTCG'); facilities = root.getCurrentScenario.getChildren().getElements(agi.stkobjects.AgESTKObjectType.E_FACILITY); satellites = root.getCurrentScenario.getChildren().getElements(agi.stkobjects.AgESTKObjectType.E_SATELLITE); for facilityIndex=0:facilities.getCount()-1 facility = facilities.get(int32(facilityIndex)); for satelliteIndex=0:satellites.getCount()-1 satellite = satellites.get(int32(satelliteIndex)); disp(strcat(char(facility.getInstanceName()), '-to-', char(satellite.getInstanceName()))) access = facility.getAccessToObject(satellite); access.computeAccess(); result = access.getComputedAccessIntervalTimes().toArray(0, -1); for i=0:result.getRowCount - 1 disp([' ', num2str(i+1), ': ', char(result.getVariant(i, 0).getString()), '-', char(result.getVariant(i, 1).getString())]); end end end root.closeScenario(); clear all