Skip to content

Instantly share code, notes, and snippets.

@ariporad
Created March 15, 2021 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariporad/710d8861bd245b350d6ee5939c718157 to your computer and use it in GitHub Desktop.
Save ariporad/710d8861bd245b350d6ee5939c718157 to your computer and use it in GitHub Desktop.
QEASim CLI Helpers
# Add this to your .bashrc or .zshrc
export MATLAB="/Applications/MATLAB_R2020a.app" # You'll need to update this to match your install of MATLAB
export MATLAB_CLI="$MATLAB/bin/matlab"
alias matlab="$MATLAB_CLI -nodesktop -nosplash"
function qeasim() {
# The above alias wasn't working in this function
$MATLAB_CLI -nodesktop -nosplash -nojvm -r "qeasim $*"
}
# Fancy wrapper to run a qeasim simualtion, then cleanly shut it down
function qearun() {
simname="$1"
echo "Running QEA Simulation: $simname..."
# Create a temporary file to put the MATLAB script in
tmp_dir="$(mktemp -d)"
mkdir -p "$tmp_dir"
tmp_file="$tmp_dir/helper.m"
# Save the script
cat > "$tmp_file" <<EOF
try
qeasim start $simname;
catch err
if (strcmp(err.message, 'Unable to resolve the name com.mathworks.mlwidgets.html.ddux.DduxWebUiEventLogger.logUIEvent.'))
% this error is totally fine, it's because we're running without a GUI
else
rethrow(err);
end
end
while 1
cmd = input("Type 'quit' to shut down the simulator, or 'escape' to get a MATLAB prompt: ", "s");
if (strcmp(cmd, 'quit'))
disp("Shutting down the simulator and exiting MATLAB...");
qeasim stop;
exit;
break;
end
if (strcmp(cmd, 'escape'))
disp("Returning to MATLAB prompt...");
disp("You MUST run 'qeasim stop' before exiting MATLAB.");
break;
end
disp("Invaild Input!");
end
EOF
# Now run it
$MATLAB_CLI -nodesktop -nosplash -nojvm -r "run('$tmp_file')"
# Then clean up
rm -r "$tmp_dir"
}

I wrote some nice wrapper functions for using MATLAB and QEASim from the terminal. QEASim is a simulation tool used in Olin College's Quantitative Engineering Analysis course. Sharing them in case anyone else wants them.

There are basically three things here:

  1. matlab: an alias which gets you the command window but in your terminal (if you later want to open full MATLAB, just type desktop)
  2. qeasim a simple script that does exactly what qeasim does it MATLAB, but in your terminal. (It does this by starting MATLAB then doing whatever you asked it to do using real qeasim.)
  3. qearun, which is a fancy wrapper around qeasim. Use it like qearun gauntlet_final, and it will start the simulation and cleanly shut it down when you're done.

Notably, qeasim and qearun are going to be way faster than doing it any other way, because they configure MATLAB to not load any UI/the JVM. It boots way quicker, and it doesn't matter because you use the in-simulator MATLAB anyways.

All of this works on Mac, and should work on Ubuntu/Linux/WSL if someone can figure out the right path to the MATLAB script. It won't work on normal windows, and as always, YMMV. Happy to help out a bit if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment