Reputation: 20319
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.
Upvotes: 9
Views: 2656
Reputation: 699
I modified Aoeuid's approach because
Ctrl+0
which jumps to the command line (and I don't see where I could set this to another value) → I replaced it with the “open file” dialog (Ctrl+O
).$PWD/$filename
instead of $filename
. You could modify his version by using open($PWD/$FILENAME)
and KP_Enter
instead of $FILENAME
and shift+Home
/control+d
.This is the result:
#!/bin/bash
filename="$1"
# 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('$PWD/$filename')"
else
## Activate window:
xdotool windowactivate --sync $MLWINDOW && \
## Press Ctrl+O to open the "open" dialog:
xdotool key --delay 0 "control+o" && \
## Wait for the file dialog to open:
sleep 0.5 && \
## Type the file name including the current directory
xdotool type --delay 0 "$PWD/$filename" && \
## Press enter:
xdotool key "KP_Enter"
fi
However, using key presses for an automated process might cause unwanted results.
Upvotes: 0
Reputation: 141
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
Upvotes: 2
Reputation: 172
Make sure that you added you folder to path.
Then you go to folder you need.
and just type in Matlab terminal
your_program_name
Then your program will run.
Upvotes: -3
Reputation: 21563
You can type the path+filename into the command line and if a matlab session is open it will open this file in the current matlab session.
Note that this only works if you make sure matlab is the default program to open this kind of file. (Tested with .m file)
Upvotes: 1