Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.0.0 - Русский


loadmatfile

loads some variables from an Octave or a Matlab MAT binary or ASCII data file

Syntax

loadmatfile(filename)
loadmatfile(filename, var1, var2, ...)
loadmatfile(format, filename, ...)
loadmatfile(filename, format, ...)
loadmatfile(filename, var1, var2, .., format)
struc = loadmatfile(.., "-toStruct")

Arguments

filename

character string containing the path of the file.

format

Explicit file format. If it is not given, loadmatfile() tries to determine the format from the file header, etc. If the data format is still unknown and the file name has the ".mat" extension, the file is considered to be a Matlab binary data file. Available explicit formats are:

"-mat"Matlab binary file.
"-octave"Octave native text file.
"-ascii" ASCII file as got from Matlab or Octave with the save -ascii option.

The format can be provided anywhere in the list of inputs.

var1, var2

Strings. Names of the variables that must be loaded from the file. If no name is provided, all the variables stored in the file are loaded. Ignored if -ascii is used.

"-toStruct

Input keyword used if the read variables must be returned as fields of an output structure, instead of directly into the calling environment.

struc

Structure whos fields are the names of variables read from the file, with their respective values.

Description

loadmatfile(filename) reads all the variables names and values stored in the given Matlab or Octave data file, and returns them in the calling environment.

struc = loadmatfile(filename, "-toStruct") does the same, but provides the read variables through the struc output structure. Any read variable varname = value can then be accessed through struc.varname. Only struc is returned to the calling environment.

This option avoids unexpectedly overwriting a variable existing in the calling environment with an homonymous variable from the file.

loadmatfile(filename, .., var2, var2, ..) reads from the file only variables whose names var1, var2, .. are provided.

loadmatfile(filename, ..,"-mat") forces the file to be read as a Matlab binary data file.

loadmatfile(filename, ..,"-octave") forces the file to be read as an Octave native text data file (default Octave format).

loadmatfile(filename, ..,"-ascii") forces the file to be read as a Matlab ASCII data file (very poor format). In this case, only one variable is read and returned. Its name is set to the basename of the file (with forbidden characters removed).

In all ways, the Octave or Matlab data types are converted into the Scilab equivalents.

Remarks

About native Octave 5.1 text data files:

  • Supported input data types: dense and sparse booleans, signed and unsigned encoded integers (from int8 to uint64), real and complex numbers (including Inf, NaN and NA special values) in sparse and dense encodings, strings, arrays of cells of strings, any cell array or hyperarray of any dimension, scalar or array of structures.
  • NA values are converted into NaN.
  • The mantissa of single precision real and complex numbers is truncated to their 8 most significant figures.
  • Any cells array of scalar strings is automatically converted into a regular Scilab array of strings.
  • In any input column of strings, appended padding spaces are left as is, without being trimmed.
  • In strings, ascii(13) characters are read as ascii(10).
  • Cells and structures must contain only supported input data types. Otherwise, an error occurs.
  • Nested containers (cells, structures) are supported.
  • Known limitation: int64 or uint64 integers bigger than 253 can't be read properly (loss of precision).

Examples

From a .mat Matlab file:

A = grand(4, 6, "uin", 0, 10);
B = sprand(10,12,0.1);
filename = TMPDIR + "/test_matfile.mat";
savematfile(filename, 'A', 'B', '-v6');
clear A B
loadmatfile(filename);
disp(A)
disp(B)

From an Octave file:

filename = pathconvert("SCI/modules/matio/tests/unit_tests/octave/octave_struct.txt");

// Read all variables, with automatic format detection:
clear ste ste2 sts stm stm2
r = loadmatfile(filename,"-toStruct")
isdef(["ste" "sts"])

// Read only a subset of stored variables:
r = loadmatfile(filename,"-toStruct", "ste", "sts")
isdef(["ste" "sts"])

// Returns the data in the calling environment:
ste = "This will be overwritten"
loadmatfile(filename,"ste", "sts");
ste, sts
--> clear ste ste2 sts stm stm2
--> r = loadmatfile(filename,"-toStruct")
 r  =
  ste: [0x0 struct] with no field

  ste2: [0x0 struct] with no field

  sts: [1x1 struct] with fields:
      r = [3.1415927,2.7182818,%i]
      b = %f
      t = ["abc","ABC"]

  stm: [2x2 struct] with fields:
        r
        b

  stm2: [2x2 struct] with fields:
        r
        b
        n

--> isdef(["ste" "sts"])
 ans  =
  F F

--> // Read only a subset of stored variables:
--> r = loadmatfile(filename,"-toStruct", "ste", "sts")
 r  =
  ste: [0x0 struct] with no field

  sts: [1x1 struct] with fields:
      r = [3.1415927,2.7182818,%i]
      b = %f
      t = ["abc","ABC"]

--> isdef(["ste" "sts"])
 ans  =
  F F

--> // Returns the data in the calling environment:
--> ste = "This will be overwritten"
 ste  =
 This will be overwritten

--> loadmatfile(filename,"ste", "sts");
--> ste, sts
 ste  =
  0x0 struct with no field

 sts  =
  r = [3.1415927,2.7182818,%i]
  b = %f
  t = ["abc","ABC"]

See also

  • load — Loads some archived variables, a saved graphic figure, a library of functions
  • savematfile — saves some Scilab variables in a Matlab MAT-file (binary or ASCII)
  • save — Saves some chosen variables in a binary data file
  • mfile2sci — Matlab M-file to Scilab conversion function
  • matfile2sci — converts a Matlab 5 MAT-file into a Scilab binary file

Bibliography

The binary mode of this function is based on the MATIO library ( http://sourceforge.net/projects/matio/ ).

History

ВерсияОписание
6.1.0
  • "-octave" option introduced.
  • Automatic input format detection added.
  • Input "-toStruct" option and output argument added.
Report an issue
<< Matlab binary files I/O Matlab binary files I/O matfile2sci >>

Copyright (c) 2022-2023 (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:
Tue Mar 07 09:29:06 CET 2023