I am trying to run a design optimisation proccess for some orbital parrameters of spacecraft. To do this, I need to be able to edit and run a GMAT script from MATLAB. Is this possible? If so, how might I go about it?

I don't need to read any outputs of GMAT. As a minimal working example, I am hoping for something like the following. Where RunGMAT is a matlab function which inserts the inclination into the GMAT script and then runs the mission.

for inclination = 0 :5 : 360



So far, I have followed the steps in the following link, however this seems to do the oppositie of what I need. It runs MATLAB functions inside a GMAT script, but I need to run GMAT scripts from MATLAB.

Any help would be greatly apreciated, Thankyou, Lee

  • $\begingroup$ Do you have an example of what you want to do, specifically with GMAT? I don’t have any experience with that in particular, but have a couple ideas depending of what the GMAT code actually looks like (and one of those is kind of hacky but could work). You can definitely run external scripts from MATLAB and a parse & edit text files pretty easily as well, so if there isn’t a binding for MATLAB & your GMAT script then that could be a starting point. $\endgroup$ Jun 4, 2023 at 12:16
  • $\begingroup$ in addition to the answer below I feel compelled to say that the example you've trotted out seems like something GMAT can do on its own based on previous questions $\endgroup$
    – Erin Anne
    Jun 5, 2023 at 0:50

2 Answers 2


In the GMAT Documentation, look for "GMAT API Reference" under your appropriate version (R2020a, where they introduced the feature, or later). An API is an "application programming interface" made to let one computer program talk to another; in this case, various programming languages including MATLAB and GMAT.

Follow the instructions in Setting up the GMAT API and you should be able to get to this point

>> addpath('/<TopLevelGMATFolder>/bin/')
>> load_gmat
No script provided to load.

ans =

Instance of GMAT Moderator is initialized. No script ready to run.

>> sat = GMATAPI.Construct('Spacecraft','MySat')

sat =

Object of type Spacecraft named MySat

>> GMATAPI.ShowObjects()

ans =

Current GMAT Objects


The SolarSystem contains the following bodies:

   [Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Luna]

I am not a GMAT user and can't answer any questions about this. I just looked for the documentation, where this is documented.


I'm not super fluent in the GMAT MATLAB API, but I am familiar with the Python API and I think most of the functional commands operate in the same way. To run what you want in python, you could do:

import sys
script_path = "/path/to/gmat/script"

from load_gmat import *  # equivalent to load_gmat

def run_gmat(inclination):                    # This would be your RunGmat function
    did_load = gmat.LoadScript(script_path)   # Load fresh for each run
    if did_load is False:                     # Check if load failed
        print('Script failed to load')

    sat = gmat.GetObject("DefaultSC")         # Can replace DefaultSC with spacecraft name
    sat.SetField('INC', inclination)          # Set the Satellites INC parameter        
    gmat.Initialize()                         # May not be necessary since loading a script

    did_run = gmat.RunScript()                # Run script with new INC value
    if did_run is False:                      # Check if run failed
        print('Script failed to run')
        print('Script ran successfully, INC=', inclination)

for inclination in range(0, 360, 5): 
    run_gmat(inclination)                    # Can iterate

This is a pretty basic implementation of looping through values. If you want to retrieve any values after a run, you can use the GetRuntimeObject call in the same way I used the GetObject call on both objects and variables.

As I mentioned, not super fluent in how the MATLAB interface works, but I assume the same function would look something like:

function did_run = RunGmat(inclination)
did_load = GMATAPI.LoadScript('/path/to/script');
if did_load == false
  disp('failed to load');

sat = GMATAPI.GetObject("DefaultSC");
sat.SetField('INC', inclination);

did_run = GMATAPI.RunScript();
if did_run == false
  disp('failed to run');
  disp("successfully ran");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.