I have thousands of lines of MATLAB code that realizes different computationally intensive operations on fMRI brain-imaging data. Many of the operations could be run in multiple parallel threads, but the problem is that all fprintf and disp output of different threads/workers is printed on the same window.
I have written a custom multithreading code that forks out new MATLAB instances using unix command in Linux environment. So, I first write out .m-files PreprocessSubj1.m, PreprocessSubj2.m, PreprocessSubj3.m and PreprocessSubj4.m, each of these contains the same computations but for different subjects. These 4 MATLAB functions are completely independent of each other.
CellArrayOfFunctions = { 'PreprocessSubj1', 'PreprocessSubj2', ...
'PreprocessSubj3', 'PreprocessSubj4' };
CellArrayOfFunctions_i = 1;
while (CellArrayOfFunctions_i <= size(CellArrayOfFunctions, 2))
FunctionToBeRun = CellArrayOfFunctions{CellArrayOfFunctions_i};
unix([ 'matlab -desktop -r ', FunctionToBeRun, ' &' ]);
CellArrayOfFunctions_i = CellArrayOfFunctions_i + 1;
end
I think using unix for forking MATLAB threads is quite a kludge. I would like to implement this code using parfor, for better maintainability and smaller memory usage. But is there any way how I could redirect fprintf (and disp) output of different threads/workers to separate windows?
Thank you 🙂
You probably want to write to /dev/pts/#, where # will denote the window. You can do this with:
You will need to determine which numbers to put after /dev/pts/ somehow though. Each time you open a new terminal, a new file appears there corresponding to that terminal, so you can check what terminals are available with
!ls /dev/pts.