How to add a custom string to a Matlab script's output?

3 views (last 30 days)
Dear community,
I have the following issue to solve. I run some data processing in a cluster environment, parallelized both across nodes and within a node with the use of parfor. The output is dumped into a single text file and there is no way to distinguish which line belongs to the processing of which data set. I can separate output from different nodes into individual log files, but not from different parallel jobs on a node, since it is treated by the cluster as a single job. My Matlab code is basically a wrapper around the data processing Matlab script. Is it possible to somehow change each output line of a Matlab script and add a specific string to the beginning of it, e.g. a dataset ID? The only solution I see is to go through the script, hunt down lines that output something and append something to it. Basically, I am looking for something like this in a bash shell:
script_name | sed 's/^/$DATA_ID/'
Best regards,
Renat.
  2 Comments
Rik
Rik on 9 Jul 2018
You could replace all occurrences of fprintf with your own function that appends your string and then calls fprintf again to write it. I would suggest you prepend the ID instead of append, as the fprintf call is likely to end with some form of a newline.
Renat
Renat on 9 Jul 2018
Well, I was hoping there is a way to avoid doing this.

Sign in to comment.

Answers (1)

Swastik Sarkar
Swastik Sarkar on 29 Nov 2024 at 12:05
Hi @Renat,
It is possible to override the built-in function responsible for displaying data to achieve the desired prefix on each output on each worker. For example, if the disp built-in function is used to display data, the following function can be defined to override it:
disp('Output')
prefix: Output
function disp(arg)
builtin('disp', ['prefix: ' arg])
end
For more information on the builtin function, refer to the following documentation:
Hope this helps printing desired info without requiring extensive modifications to the entire code.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!