How do I connect STK and Matlab?


QuestionHow do I connect STK and Matlab?
There are two ways to talk between STK and Matlab: 
  1. COM
  2. TCP/IP
COM is the preferred way to connect STK and Matlab since it is very reliable, works for any combination of STK and Matlab (even 64 bit), and does not require any additional installations. The TCP/IP connection is only required if you need Matlab plugins.

Below are the steps to connect via COM. For TCP/IP you will need to install the Matlab connectors.

To connect via COM.

1) Establish a connection between STK and Matlab

Option 1: If STK is not running yet, start STK
stk = actxserver('STK.Application');
stk.Visible = true;
Option 2: If STK is already running, connect to it
stk = actxGetRunningServer('STK.Application');

2) Get the root object 
root = stk.personality2; 
You can now control STK from Matlab. Again, there are two options to do the same thing.
Option 1: Using STK Connect
root.ExecuteCommand('New / Scenario MatlabTest');
root.ExecuteCommand('New / */Facility AGI_HQ');
root.ExecuteCommand('SetPosition */Facility/AGI_HQ Geodetic 40.0386 -75.5966 0.0');

Option 2: Using the STK Object Model
agi_HQ = root.CurrentScenario.Children.New('eFacility','AGI_HQ'); 
agi_HQ.Position.AssignGeodetic(40.0386, -75.5966, 0.0);

Attached is a sample .m file to show you every way you can connect to STK from Matlab. 
Resource File 1
Resource File 2