1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2010 - INRIA - Vincent COUVERT
3 // Copyright (C) ???? - INRIA - Serge STEER
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11 function loadmatfile(varargin)
12 // Loads variables in a Matlab binary or ASCII file into Scilab
13 // This function has been developed following the 'MAT-File Format' description:
14 // www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf
16 // Verify that all inputs are character strings
17 for k=1:size(varargin)
18 if type(varargin(k))<>10 then
19 error(msprintf(gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"),"loadmatfile"));
31 fileExtension = fileparts(fil, "extension");
32 if isempty(fileExtension) then // No extension: looks for a file named fil.mat and treats it as a binary MAT-file
35 elseif convstr(fileExtension, "l") <> ".mat" then // Extension other than .mat: treats the file as ASCII data.
37 else // Compatibility with old loadmatfile version
40 else // Try to find type binary or ASCII ?
41 // Filename is the first parameter: loadmatfile(filename[,opts])
42 // or the second parameter: loadmatfile(filetype,filename[,opts]) with filetype equal to -ascii or -mat
43 // filetype can also be included in opts
45 while k<=lstsize(varargin)
54 warning(msprintf(gettext("%s: This feature has not been implemented: %s."),"loadmatfile","-regexp"));
55 while k<=lstsize(varargin) & and(varargin(k)<>["-mat","-ascii"])
59 if isempty(fil) then // Filename
60 fil=pathconvert(varargin(k),%f,%t);
61 if fileparts(fil,"extension")==".mat" & isempty(bin) then // extension .mat and bin not already fixed by options
63 elseif isempty(bin) then
66 else // Variable names
67 varnames=[varnames;varargin(k)]
77 if fileparts(fil,"extension")=="" then
81 // --- BINARY FILE ---
82 if bin then // Uses MATIO interface
88 //-- Try to open the file
89 fd = matfile_open(pathconvert(fil, %F, %T), "r");
91 error(msprintf(gettext("%s: Cannot open file %s.\n"),"loadmatfile",fil));
94 //-- Read first variable
95 [Name, Matrix, Class] = matfile_varreadnext(fd);
97 //-- Loop on the stored variables
100 // Old version compatibility | Name has been given
101 if isempty(varnames) | or(Name==varnames) then
102 Names=[Names,Name];Matrices($+1)=Matrix
105 [Name, Matrix, Class] = matfile_varreadnext(fd);
112 //-- Error while reading?
113 if isempty(Names) then
114 error(msprintf(gettext("%s: No variable read in file ''%s''. Check if your file is not corrupted.\n"),"loadmatfile",fil));
117 //-- Return variables in the calling context
118 execstr('['+strcat(Names,',')+']=resume(Matrices(:))')
120 // --- ASCII FILE ---
125 rowIndexes = grep(txt, "%")
127 txt(k) = part(txt(k), 1:(strindex(txt(k), "%") - 1));
133 // Output variable name generated from file name
134 name = fileparts(fil, "fname");
136 execstr(name + " = resume(mat)")