## MATLAB<->GMAT example

MATLAB-specific help discussions.

### MATLAB<->GMAT example

Attached is an example I created for using GMAT from within MATLAB, and getting GMAT-generated results back into your MATLAB script.

The script sets up an initial spacecraft state and uses GMAT to propagate it forward for 1 day, then extracts the new state for further processing within MATLAB.

This was created to answer a question I was getting along these lines; if there is a problem with this method, please let me know.

Code: Select all
`% 1. Open GMAT and MATLAB.% 2. Make sure your MATLAB path includes the GMAT matlab folder and all its%   subfolders.% 3. Start GMAT's MATLAB server and keep it running in the background.% 4. Run this function from MATLAB.function driver()state = [    -4453.783586    -5038.203756    -426.384456    3.831888    -2.887221    -6.018232    ];nextState = prop(state);nextStateendfunction nextState = prop(state)%% Initialize GMATOpenGMAT();ClearGMAT();%% Set up the scenarioCreate Spacecraft ISS;GMAT ISS.J2000BodyName = Earth;GMAT ISS.Epoch.UTCGregorian = 01 Jun 2004 12:00:00.000;GMAT ISS.DisplayStateType = Cartesian;GMAT ISS.CoordinateSystem = EarthMJ2000Eq;GMAT(['ISS.X = ' num2str(state(1), '%f')]);GMAT(['ISS.Y = ' num2str(state(2), '%f')]);GMAT(['ISS.Z = ' num2str(state(3), '%f')]);GMAT(['ISS.VX = ' num2str(state(4), '%f')]);GMAT(['ISS.VY = ' num2str(state(5), '%f')]);GMAT(['ISS.VZ = ' num2str(state(6), '%f')]);GMAT ISS.Cd = 2.2;GMAT ISS.Cr = 1.2;GMAT ISS.DragArea = 20;GMAT ISS.SRPArea = 20;GMAT ISS.DryMass = 1000;GMAT ISS.TotalMass = 1000;Create ForceModel Earth2Body;GMAT Earth2Body.PrimaryBodies = {Earth};GMAT Earth2Body.Drag = None;GMAT Earth2Body.SRP = Off;GMAT Earth2Body.Gravity.Earth.Model = JGM2;GMAT Earth2Body.Gravity.Earth.Degree = 0;GMAT Earth2Body.Gravity.Earth.Order = 0;GMAT Earth2Body.PointMasses   = {};Create Propagator RKV89;GMAT RKV89.FM = Earth2Body;GMAT RKV89.Type = RungeKutta89;GMAT RKV89.InitialStepSize = 5;GMAT RKV89.Accuracy = 1e-013;GMAT RKV89.MinStep = 5;GMAT RKV89.MaxStep = 5;GMAT RKV89.MaxStepAttempts = 50;Propagate(['RKV89(ISS) {ISS.ElapsedSecs = ' num2str(60*60*24) '}']);%% Run the scenarioBuildRunGMAT();%% Get result back outsc = GetGMATObject('ISS');nextState = [    sc.X    sc.Y    sc.Z    sc.VX    sc.VY    sc.VZ    ];end`
Attachments driver.m
A GMAT driver function in MATLAB
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker

Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

### Re: MATLAB<->GMAT example

Just FYI, a similar post was made in the following:
http://gmat.ed-pages.com/forum/viewtopic.php?f=14&t=10
space_egd

Posts: 357
Joined: Sun Jun 08, 2008 9:02 pm
Location: MD 