summaryrefslogtreecommitdiff
path: root/modules/matio/macros/loadmatfile.sci
blob: 48793a4bfb26072f3a74cc7ced5b4f18997271ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2010 - INRIA - Vincent COUVERT
// Copyright (C) ???? - INRIA - Serge STEER
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function loadmatfile(varargin)
    // Loads variables in a Matlab binary or ASCII file into Scilab
    // This function has been developed following the 'MAT-File Format' description:
    // www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf

    // Verify that all inputs are character strings
    for k=1:size(varargin)
        if type(varargin(k))<>10 then
            error(msprintf(gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"),"loadmatfile"));
        end
    end

    [lhs,rhs]=argn(0)

    fil=[]
    bin=[]
    varnames=[]

    if rhs==1 then
        fil = varargin(1)
        fileExtension = fileparts(fil, "extension");
        if isempty(fileExtension) then // No extension: looks for a file named fil.mat and treats it as a binary MAT-file
            bin = %T
            fil = file + ".mat";
        elseif convstr(fileExtension, "l") <> ".mat" then // Extension other than .mat: treats the file as ASCII data.
            bin = %F
        else // Compatibility with old loadmatfile version
            bin = %T
        end
    else // Try to find type binary or ASCII ?
        // Filename is the first parameter: loadmatfile(filename[,opts])
        // or the second parameter: loadmatfile(filetype,filename[,opts]) with filetype equal to -ascii or -mat
        // filetype can also be included in opts
        k=1
        while k<=lstsize(varargin)
            select varargin(k)
            case "-mat"
                bin=%T
                k=k+1
            case "-ascii"
                bin=%F
                k=k+1
            case "-regexp"
                warning(msprintf(gettext("%s: This feature has not been implemented: %s."),"loadmatfile","-regexp"));
                while k<=lstsize(varargin) & and(varargin(k)<>["-mat","-ascii"])
                    k=k+1
                end
            else
                if isempty(fil) then // Filename
                    fil=pathconvert(varargin(k),%f,%t);
                    if fileparts(fil,"extension")==".mat" & isempty(bin) then // extension .mat and bin not already fixed by options
                        bin=%T
                    elseif isempty(bin) then
                        bin=%F
                    end
                else // Variable names
                    varnames=[varnames;varargin(k)]
                end
                k=k+1
            end
        end
        if isempty(bin) then
            bin=%T
        end
    end

    if fileparts(fil,"extension")=="" then
        fil=fil+".mat"
    end

    // --- BINARY FILE ---
    if bin then // Uses MATIO interface

        Name="";
        Names=[];
        Matrices=list()

        //-- Try to open the file
        fd = matfile_open(pathconvert(fil, %F, %T), "r");
        if fd==-1 then
            error(msprintf(gettext("%s: Cannot open file %s.\n"),"loadmatfile",fil));
        end

        //-- Read first variable
        [Name, Matrix, Class] = matfile_varreadnext(fd);

        //-- Loop on the stored variables
        while Name<>""

            // Old version compatibility | Name has been given
            if isempty(varnames) | or(Name==varnames) then
                Names=[Names,Name];Matrices($+1)=Matrix
            end

            [Name, Matrix, Class] = matfile_varreadnext(fd);

        end

        //-- Close the file
        matfile_close(fd)

        //-- Error while reading?
        if isempty(Names) then
            error(msprintf(gettext("%s: No variable read in file ''%s''. Check if your file is not corrupted.\n"),"loadmatfile",fil));
        end

        //-- Return variables in the calling context
        execstr("["+strcat(Names,",")+"]=resume(Matrices(:))")

        // --- ASCII FILE ---
    else
        txt = mgetl(fil);

        // Remove comments
        rowIndexes = grep(txt, "%")
        for k = rowIndexes
            txt(k) = part(txt(k), 1:(strindex(txt(k), "%") - 1));
        end

        // Values read
        mat = evstr(txt);

        // Output variable name generated from file name
        name = fileparts(fil, "fname");

        execstr(name + " = resume(mat)")
    end
endfunction