4

I have a long-running Matlab script for data processing. I want to send it a flag over stdin to tell it I have new data to process. I also want to read a flag from stdout when it is done processing.

In other words, I have a Process A that sends a flag about once a minute to Matlab. I want Matlab to wait until it receives this flag.

Writing to stdout in a matlab process is as easy as calling fprintf. But how can I read from stdin? Documentation on fopen doesn't mention an input pipe, and neither does fread. How can get a Matlab script to read from stdin?

2
  • How do you want to inform your script? Should it poll on a regular basis or should a callback function be executed when you enter a word? Commented May 26, 2014 at 18:34
  • The script should either poll stdin to see if anything has been written or perform a blocking read on stdin. Either would be sufficient. (Though using Matlab's input builtin would not work because I think it reads directly from the keyboard, not stdin.) Commented May 26, 2014 at 18:38

2 Answers 2

4

It actually turns out that the solution is as simple as input. Write the following into myscript.m:

str = input('', 's'); 
fprintf(str); 
exit;

Then, run the following in a shell:

echo Hello world | matlab -nosplash -nodisplay -nodesktop -r "myscript"

Indeed, we see that "Hello world" is printed to the console, along with Matlab's startup text.

So, in summary, input reads from stdin, and fprintf writes to stdout.

Sign up to request clarification or add additional context in comments.

2 Comments

Can input with newlines be read in this manner without resorting to a device like looping until EOF? The input command ceases to read when it encounters the first newline character. I am trying to pipe tabulated data to a matlab script without writing to a temp file or a fifo. Thanks.
If you don't want to loop until you see a sentinel character like EOF, then I think the best way to accomplish what you mean to do would be to always send the number of rows in your data file as the first line to stdin. Then, say you have 5 rows, you can just call input again 5 times in a for loop.
2

One way to do this is using a named pipe. In a shell, run mkfifo MY_PIPE to make a named pipe. This will make a file-like object called MY_PIPE that you can read and write to. Then, redirect the output of the program sending data to MY_PIPE, e.g. ./program.sh > MY_PIPE. Finally, to read from the pipe in Matlab, you can use fopen('MY_PIPE', 'r').

Note that answer is limited in a few ways:

  1. It will only run in a Linux environment and with access to the shell.

  2. It doesn't make use of Matlab built-ins and is kind of hackish.

1 Comment

If somebody has a solution that makes use of Matlab builtins instead of shell directives PLEASE let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.