2
votes

I have a Matlab program that runs different unix commands fairly often. For this question let's assume that what I'm doing is:

unix('ls test')

It happens to me quite frequently that I accidentally press a key(like enter or the arrow keys) f.e. when I'm waking up my display from standby. In theory this shouldn't interfere with the unix command. Though unfortunately, Matlab will take this input and forward it right into the execution of the command. The above command then becomes something like this:

unix('ls te^[0Ast')

(Side note: ^[0A is the hex representation of the linefeed character)

Obviously, this will produce an error.

Does anyone have an idea how to work around this issue?

I was thinking that there might be a way to start Matlab with my script in a way that doesn't forward any user input from within the unix shell.

#!/bin/bash
matlab -nodisplay -nosplash -r "runMyScript();"

Can I somehow pipe the user-input somewhere else and isolate Matlab from any sort of input?

1

1 Answers

2
votes

That is not very specific question, but let me try. I can see several options. I am assuming that matlab is text terminal application.

  1. There is nohup(1) command. Since you use linux, chances are that there is non-posix version if it which says in it's man page: If standard input is a terminal, redirect it from /dev/null.

    $ nohup matlab -nodisplay -nosplash -r "runMyScript();"
    
  2. You can redirect /dev/null yourself

    $ matlab -nodisplay -nosplash -r "runMyScript();" < /dev/null
    

    But matlab can actually re-open it's stdin ignoring what you piped into it (for example ssh does that, you can't use echo password | ssh somewhere.

  3. if you are running in graphics environment you may want to minimise the window, so that it does not receive any input. Probably not your case, you would figure out yourself :)

  4. you may try to wake up by hitting "Ctrl", similar key or mouse

  5. You may run matlab in screen(1) command disconnect from the screen or switch to different window. Screen is a program allowing you to create virtual terminals (similar to virtual desktops in GUI). If you haven't heard of screen, I suggest you to look at some tutorials. Googling for gnu screen tutorial seems to offer quite a few.