If I already have an instance of Matlab running is it possible to tell open a file in the Matlab editor from outside the Matlab application? I'm wondering if there it is possible do something like this.
Launch an instance of Matlab
$ ./matlab
Open a file for editing using an already running instance of Matlab:
$ matlab_open_file.sh theFile.m
The GUI variant is dragging a file from a folder and then dropping it onto Matlab icon (this actually works under OS X)
Note I know that you can launch Matlab and have it immediately execute a command (you could use this to start the editor on launch). This is not what I want.
How to run the m-file? After the m-file is saved with the name filename. m in the current MATLAB folder or directory, you can execute the commands in the m-file by simply typing filename at the MATLAB command window prompt.
You can use the MATLAB® command-line interface as an alternative to using the Simulink® UI. Enter commands directly in the MATLAB Command Window or save them in a script file.
On the Home tab, in the File section, click Open , and then select a file to open. You also can double-click the file in the Current Folder browser.
I scripted a workaround for Linux (functional on Mint 17.1 with R2014a and R2014b), which I then associated with the .fig
and .m
file extensions. Note that this requires xdotool
to be installed, and the keystrokes are set for Windows shortcuts (by default, MATLAB ships with Emacs shortcuts on Linux, but virtually everyone changes them in my experience). This has the limitation that any text currently on the command line is erased, and there is a small window of time where MATLAB must not lose focus. But in the absence of a more robust solution, it works well enough for me.
#!/bin/bash
# Hacky way to open a MATLAB figure in an existing instance if there is
# one, and start a new instance if not.
# What are we trying to open?
FILENAME="$@";
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$FILENAME')"
else
# We use the first existing instance since MATLAB is open
set -- $MLWINDOW
# Jump to the command line and erase it
xdotool windowactivate --sync $1 key --delay 0 "control+0" Escape
# Put the filename on the command line
xdotool type --delay 0 "$FILENAME"
# Select the filename and press ctrl-D to open, then clean up command line
xdotool key --delay 0 "shift+Home" "control+d" Escape
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With