2

I'm currently using an external editor of Matlab .m files, with a custom build system that calls Matlab from the command line to run the Matlab script (with the -nosplash and -nodesktop). However this creates two problems:

1) Matlab closes right after running the script: any windows or plots I call in the script are closed right after running the script, which obviously happens in a matter of seconds.

2) There is a slight delay every time I run the script because Matlab is effectively being started from scratch.

So I was wondering if would be possible to have Matlab running in the background, and just running the scripts whenever I want?

I'm running Linux 64bits, Matlab 2013a, and Sublime Text 3.

EDIT: I've testing the setup with a basic script:

a=5;
figure
plot(a);

EDIT2: I'm calling Matlab through a Sublime Text build system that runs:

matlab -nosplash -nodesktop <[script].m
joaocandre
  • 1,621
  • 4
  • 25
  • 42
  • Please provide your shell script/call to the script. I don't remember MATLAB quitting after running a script using `-nodesktop`. – knedlsepp Apr 07 '15 at 14:48
  • Edited my answer. I doubt the issue is with the script though. – joaocandre Apr 07 '15 at 15:04
  • I was talking about the bash/sh/whatever-shell-call you use to open MATLAB with. I also doubt the MATLAB script is the issue ;-) – knedlsepp Apr 07 '15 at 15:57
  • Sorry, my bad. I've added it now. – joaocandre Apr 07 '15 at 16:10
  • The reason MATLAB quits is probably because using the stream `< filename.m` sends an EOF to the MATLAB command. – knedlsepp Apr 07 '15 at 17:08
  • Have you tried something like [this](http://serverfault.com/questions/443297/write-to-stdin-of-a-running-process-using-pipe)? I don't have a Sublime setup, but it seems to work from a different shell: http://pastebin.com/bXSXWs04 – knedlsepp Apr 07 '15 at 17:59
  • possible duplicate of [Creating custom build system for MATLAB through Sublime Text 2](http://stackoverflow.com/questions/11869586/creating-custom-build-system-for-matlab-through-sublime-text-2) – knedlsepp Apr 07 '15 at 18:15

1 Answers1

0

There is no way to have Matlab running in the background and "just running the scripts whenever you want" without having an interactive session open somewhere.

Suppose that your system has a custom wrapper matlab-wrapper that is used to submit scripts in the background. You would call your script like this:

$ matlab-wrapper myscript.m

Likely, matlab-wrapper is doing something like this:

#!/bin/bash
/apps/matlab14a/bin/matlab -nodesktop -nosplash -r run\ "$1",exit

Or even more, submitting the above script to a scheduler via qsub or some other command.

The key would be modify the wrapper script to find the part where the Matlab binary is actually invoked. If your system allows, you could copy the wrapper script and modify it. (Either by simply removing the -r run\ "$1" text or something more complicated.) Then, you should be able to launch an interactive version of Matlab per the custom configuration on your system, and call your scripts from the Matlab command window.

Micah Smith
  • 4,203
  • 22
  • 28
  • So I would need to write a script that finds the PID/identifier of a running matlab session and submit the .m to run, right? I guess Matlab foruns would be a more appropriate place to research about this. – joaocandre Apr 07 '15 at 15:52
  • Again, what does "running Matlab session" mean? The only way I know of to issue multiple commands interactively to a single Matlab session is for it to be an interactive session and you type commands into the Matlab command window. – Micah Smith Apr 07 '15 at 15:53
  • That assumes I'm using the Matlab GUI. I'd like to just use the CLI, running with `nodesktop` option. I don't known if its possible though, and running `(matlab -nodesktop)&` it just gets stuck in the splash image. – joaocandre Apr 07 '15 at 16:03
  • That does not assume you're using the Matlab GUI. You should be able to invoke Matlab as `matlab -nodesktop -nosplash`. What is the reason to be trying to background this process? – Micah Smith Apr 07 '15 at 17:04