Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - Français


fscanfMat

reads a matrix from a text file

Syntax

M = fscanfMat(filename[, fmt]);
[M, text] = fscanfMat(filename [, fmt]);

Arguments

filename

a character string giving the name of the file to be scanned.

fmt

a character string giving the format. This is an optional parameter, the default value is "%lg".

Supported format: %[width].[precision]type or %type where:

width: optional number that specifies the minimum number of characters output;

precision: optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values.

type: lf, lg, d, i, e, f, g (see scanf_conversion).

M

output variable. A matrix of real numbers.

text

output variable. A column vector of strings. It is comments in the beginning of the file filename.

Description

The fscanfMat function is used to read a real matrix from a text file. The first non-numeric lines of the file are returned in text if requested and all the remaining lines must have the same number of columns (column separator are assumed to be either white spaces or tab characters, if there are the both, white spaces are chosen by default). The number of columns of the matrix will follow the number of columns found in the file and the number of lines is fetched by detecting eof in the input file. This function can be used to read back numerical data saved with the fprintfMat (default separator used is a space).

fscanfMat supports files encoded as ANSI/ASCII and UTF-8.

To read a CSV formatted file, the csvRead function is recommended.

Examples

fd = mopen(TMPDIR + "/Mat", "w");
mfprintf(fd, "Some text.....\n");
mfprintf(fd, "Some text again\n");
a = rand(6,6);
for i = 1:6 ,
for j = 1:6, mfprintf(fd, "%5.2f ", a(i,j));end;
mfprintf(fd, "\n");
end
mclose(fd);
a1 = fscanfMat(TMPDIR + "/Mat")

A = ones(5,5) + 0.1;
fprintfMat(TMPDIR + "/Matrix.txt", A, "%lg");
B = fscanfMat(TMPDIR + "/Matrix.txt", "%lg");
A == B

C = [%nan, %inf, -%inf, 1];
fprintfMat(TMPDIR + "/MatrixNANINF.txt", C);
D = fscanfMat(TMPDIR + '/MatrixNANINF.txt')

See also

  • scanf_conversion — mscanf, msscanf, mfscanf conversion specifications
  • mclose — closes opened files
  • meof — checks if end of file has been reached
  • mfprintf — converts, formats, and writes data to a file
  • fprintfMat — writes a matrix in a file
  • mfscanf — reads input from the stream pointer stream (interface to the C fscanf function)
  • mget — parses numbers in a binary file and returns them as decimals
  • mgetstr — reads a character string from a file
  • mopen — opens files in Scilab
  • mprintf — converts, formats, and writes data to the main scilab window
  • mput — writes byte or word in a given binary format
  • mputstr — write a single text in an open file
  • mseek — sets current position in a binary file
  • mtell — Returns the offset of the current byte relative to the beginning of a file
  • mdelete — deletes file(s)
  • csvRead — Read comma-separated value file
Report an issue
<< fprintfMat Entrées/Sorties [fichiers] getdeprecated >>

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:
Thu Oct 24 11:15:59 CEST 2024