Using fprintf to write to multiple files simultaneously

14 Ansichten (letzte 30 Tage)
Mirko
Mirko am 9 Apr. 2020
Bearbeitet: Walter Roberson am 26 Jul. 2022
I want to write the same text to the screen and in to a file. So I have to double the code:
fprintf('this is some text with numbers: %d, %d', x,y);
fprintf(fid, 'this is some text with numbers: %d, %d', x,y);
Alternatively I can introduce a text string to avoid double code:
str = sprintf('this is some text with numbers: %d, %d', x,y);
fprintf(str);
fprintf(fid, str);
But obviously it does not work to use fprintf to write to mutliple files simultaneously? Would be nice if possible.
fprintf([1 fid],'this is some text with numbers: %d, %d', x,y);

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 9 Apr. 2020
Bearbeitet: Ameer Hamza am 9 Apr. 2020
As far as I know, this feature is not natively supported by MATLAB. But if it is really important for your application, then you can try the following two methods.
First, create a custom function which takes an array of FIDs and call fprintf one by one on each of them. Save the following function in a file printf.m in MATLAB's path.
function printf(fid, varargin)
for f = fid(:)'
fprintf(f, varargin{:});
end
end
Call this function
f = fopen('test.txt', 'w+');
printf([1 f 1 1 f], 'hello world\n');
fclose(f);
Second, overload the fprintf function. Create a folder name @double in your MATLAB's path and save the following function in file fprintf.m
function y = fprintf(fid, varargin)
y = zeros(size(fid));
for i = 1:numel(fid)
y(i) = builtin('fprintf', fid(i), varargin{:});
end
end
[Important note:] The definition of overloaded function is updated; the first code can break the compatibility in some cases.
Call this function
f = fopen('test.txt', 'w+');
fprintf([1 f 1 1 f], 'hello world\n');
fclose(f);
Note that this overloading will not break compatibility, other programs calling fprintf() will still work normally.
As a side note, these functions can also be written as one-liners without for-loop:
printf.m:
function printf(fid, varargin)
arrayfun(@(f) fprintf(f, varargin{:}), fid);
end
fprintf.m
function y = fprintf(fid, varargin)
y = cell2mat( arrayfun(@(f) {builtin('fprintf', f, varargin{:})}, fid) );
end
  2 Kommentare
Mirko
Mirko am 9 Apr. 2020
Thank you very much for your detailed and useful response!
Ameer Hamza
Ameer Hamza am 9 Apr. 2020
Glad to be of help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Apr. 2020
You are correct, any one fprintf() can only write to one place at a time.
You should have a look at diary
  3 Kommentare
Steven Lord
Steven Lord am 26 Jul. 2022
You could try launching MATLAB with the -logfile option and also call diary in that session. The two files won't be exactly the same but they may be close enough for your purposes.
But for problems like the original question and for this, if I wanted to log information to multiple places I'd write a function that I could call with a vector of file identifiers (and potentially some "dummy" identifier if I wanted to write to the screen as well) that would write to all those locations.
function logToFilesAndScreen(fid, varargin)
% logToFilesAndScreen Write text to one or more files and to the screen
%
% logToFilesAndScreen(fid, varargin) writes the information stored in
% varargin (which must contain valid inputs to the fprintf function when
% passed to it as a comma-separated list) to the screen and to all the
% files whose file IDs (as opened using fopen) are passed in as the fid
% input.
% Print to screen
fprintf(varargin{:})
% Print to file(s)
for whichfile = 1:numel(fid)
fprintf(fid(whichfile), varargin{:})
end
end
Walter Roberson
Walter Roberson am 26 Jul. 2022
Bearbeitet: Walter Roberson am 26 Jul. 2022
Note: identifier 1 always writes to the screen (well, unless MATLAB has been run in non-interactive mode.)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by