Can I open a file in an executable instance of Matlab from the command line?

If I already have a Matlab launch instance, can I open the file in the Matlab editor from outside the Matlab application? I am wondering if something like this can be done.

Run Matlab Instance

$ ./matlab 

Open the file for editing using an already running Matlab instance:

 $ matlab_open_file.sh theFile.m 

The GUI option drags the file from the folder and then drops it onto the Matlab icon (this actually works under OS X)

Note. I know that you can start Matlab and execute the command immediately (you can use it to launch the editor on startup). This is not what I want.

+8
matlab
source share
4 answers

I wrote a script for Linux (functional on Mint 17.1 with R2014a and R2014b), which I then associated with the .fig and .m . Please note that this requires the installation of xdotool , and the keys for Windows shortcuts (by default, MATLAB comes with Emacs shortcuts on Linux, but almost all change them for my experience). This has the limitation that any text on the command line is now erased, and there is a small window of time when MATLAB should not lose focus. But in the absence of a more reliable 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 
+2
source share

You can enter the path + file name on the command line, and if the matlab session is open, it will open that file in the current matlab session.

Please note that this only works if you make sure that Matlab is the default program to open such a file. (Tested with .m file)

+1
source share

I changed the Aoeuid approach because

  • this did not work for me, since I reassigned Ctrl+0 , which jumps to the command line (and I don’t see where I can set it to another value) β†’ I replaced it with the "open file" dialog ( Ctrl+O ).
  • I might want to open scripts that are not in the current matlab path -> I use $PWD/$filename instead of $filename . You can change its version 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 keystrokes for an automatic process can lead to undesirable results.

0
source share

Make sure you add the folder to the path.

Then go to the desired folder.

and just enter the matlab terminal

 your_program_name 

Then your program will start.

-2
source share

All Articles