Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab command (from bash / command line) on an already running session

Tags:

bash

matlab

$ matlab -nodesktop -nojvm &

How would I execute matlab commands on the session that was just created?

In other words, I want to have a matlab session running in the background, and execute matlab commands and/or scripts from an arbitrary terminal at any given time without having to create a new session.

like image 972
kDawson Avatar asked Sep 06 '12 18:09

kDawson


2 Answers

A possibility is to start a screen session, then start matlab on it, and detach from it. Anytime you want to use it, just fire up a terminal and reattach that screen session.

Basically start screen (just type screen at a terminal), and start your matlab session. Then detach from the session (Ctrl+A followed by pressing D) and you'll be back to your terminal. You can close the window no problem, any process that started on screen will keep on running. Whenever you want to get it again (it's called reattach the session), just use screen -r. Take a look at the man page for all the other options.

Note that a screen session can have any number of windows and you can also have multiple screen session at the same time. Take a good luck at some tutorials online, it's an extremely useful tool, specially but not only, if you connect a lot to other systems that may have need to run long jobs.

like image 33
carandraug Avatar answered Oct 15 '22 02:10

carandraug


I would suggest a similar solution as carandraug did, only I prefer tmux as the multiplexer. It may be a bit tricky getting the commands passed in correctly so create a shell-script that handles the details.

Let's say you've started matlab in a terminal like this:

tmux new -s matlab "matlab -nodesktop -nojvm"

Now a tmux session called matlab is running matlab with no gui.

Create this shell-script:

mx

#!/bin/bash

if [[ $# -eq 0 ]]; then
  while read; do
    tmux send-keys -t matlab "$REPLY"$'\n'
  done
else
  tmux send-keys -t matlab "$@"$'\n'
fi

In a different terminal you can now run quoted matlab commands:

mx "A = reshape(1:9, 3, 3)"

Or even pass commands in through a pipe:

for mat in A B C; do echo "$mat = reshape(1:9, 3, 3)"; done | mx
like image 90
Thor Avatar answered Oct 15 '22 03:10

Thor