Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - English


mfprintf

converts, formats, and writes data to a file

Syntax

mfprintf(fd, format, a1, ..., an)

Arguments

fd

scalar positive index identifying the file, as provided by mopen.

If fd equals 0: redirection to stderr.

If fd equals 6: redirection to stdout (console).

format

a string providing the format to use to write all next arguments. The format follows -- as close as possible -- the C printf format operand syntax, as described in the printf_conversion page. UTF-8 extended characters are supported.

Numbered placeholders "%n$.." are not supported.

a1, ..., an

Data to be converted and written according to the format argument. Supported types: all numbers, booleans, strings. Only the real part of complex numbers is considered (current Scilab limitation).

Description

mfprintf(fd, format, a1, a2, ..) replaces placeholders provided in the format string with values of a1, a2, .. converted according to each respective placeholder directive, and writes the result to the file specified by its index fd.

If a1, a2, .. are arrays with multiple rows, they feed the format row by row: the format is used iteratively for every row of the (horizontally "concatenated") arrays, until the bottom of the least tall array is reached. Remaining rows of taller arrays (if any) are ignored. See examples.

If the total number of columns of a1, a2, .. is bigger than the number of placeholders in the format, then extra columns are ignored. If it is smaller, an error is yielded.

The mfprintf function is a interface for C-coded fprintf function.

Examples

fd = mopen(TMPDIR+'/text.txt','wt');
mfprintf(fd,'hello %s %d.\n','world',1);
mfprintf(fd,'hello %s %d.\n','scilab',2);
mfprintf(fd,'This line is built with a column vector (26:28) %d.\n',[26:28].');
mfprintf(fd,'This line is built with a row vector (26:28) %d.\n',[26:28]);
A = rand(3,6);
mfprintf(fd,'This line is built with a matrix %.3f.\n',A);
mclose(fd);
editor(TMPDIR+'/text.txt')

mfprintf(0,'stderr output.\n');
mfprintf(6,'stdout output.\n');

I = (1:4)';
A = [26.93 ; 63.25 ; 40.51 ; 91.84];
B = [ 3.62 ; 15.04 ; 25.3  ; 48.19];
C = [ 4.37   28.06
     48.18   %inf
     41.48   %nan
     26.39   77.83];
Status = ["NOK" "NOK" "NOK" "OK"]';
Format = "Iteration %d: Results: A= %f   B= %2d%%  Status= %3s   C(1)= %g  C(2)= %e\n";
File = TMPDIR + "/mfprintf.txt";
fd = mopen(File, 'wt');
mfprintf(fd, Format, I, A, B, Status, C);
mclose
editor(File)
Iteration 1: Results: A= 26.930000   B=  3%  Status= NOK   C(1)= 4.37  C(2)= 2.806000e+01
Iteration 2: Results: A= 63.250000   B= 15%  Status= NOK   C(1)= 48.18  C(2)= Inf
Iteration 3: Results: A= 40.510000   B= 25%  Status= NOK   C(1)= 41.48  C(2)= Nan
Iteration 4: Results: A= 91.840000   B= 48%  Status=  OK   C(1)= 26.39  C(2)= 7.783000e+01

Supernumerary columns or rows are ignored:

A = [%T  %F  %T  %T  %F]';
B = [ 4.37   28.06
     48.18   %inf
     41.48   %nan ];
File = TMPDIR + "/mfprintf.txt";
fd = mopen(File, 'wt');
mfprintf(fd, "OK? %s  Value: %4.1f\n", A, B)
mclose
editor(File)
OK? T  Value:  4.4
OK? F  Value: 48.2
OK? T  Value: 41.5

See also

  • printf_conversion — mprintf, msprintf, mfprintf C-format specifications
  • csvWrite — Write comma-separated value file
  • fprintfMat — writes a matrix in a file
  • write — writes real numbers or strings in the console or in a formatted file
  • mputl — writes strings in a text file
  • mputstr — write a single text in an open file
  • mopen — opens files in Scilab
  • mclose — closes opened files
  • msprintf — converts, formats, and writes data to the main scilab window
  • mfscanf — reads input from the stream pointer stream (interface to the C fscanf function)

History

VersionDescription
6.1.1 Input data can be boolean.
Report an issue
<< merror Files : Input/Output functions mfscanf >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jun 17 17:49:16 CEST 2024