a b/functions/adminfunc/eeg_readoptions.m
1
% EEG_READOPTIONS - Read EEGLAB memory options file (eeg_options) into a
2
%                     structure variable (opt).
3
%
4
% Usage:
5
%   [ header, opt ] = eeg_readoptions( filename, opt );
6
%
7
% Input:
8
%   filename    - [string] name of the option file
9
%   opt         - [struct] option structure containing backup values
10
%
11
% Outputs:
12
%   header      - [string] file header.
13
%   opt         - [struct] option structure containing an array of 3 fields
14
%                 varname     -> all variable names.
15
%                 value       -> value for each variable name
16
%                 description -> all description associated with each variable
17
%
18
% Author: Arnaud Delorme, SCCN, INC, UCSD, 2006-
19
%
20
% See also: EEG_OPTIONS, EEG_EDITOPTIONS
21
22
% Copyright (C) Arnaud Delorme, SCCN, INC, UCSD, 2006-
23
%
24
% This file is part of EEGLAB, see http://www.eeglab.org
25
% for the documentation and details.
26
%
27
% Redistribution and use in source and binary forms, with or without
28
% modification, are permitted provided that the following conditions are met:
29
%
30
% 1. Redistributions of source code must retain the above copyright notice,
31
% this list of conditions and the following disclaimer.
32
%
33
% 2. Redistributions in binary form must reproduce the above copyright notice,
34
% this list of conditions and the following disclaimer in the documentation
35
% and/or other materials provided with the distribution.
36
%
37
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
47
% THE POSSIBILITY OF SUCH DAMAGE.
48
49
function [ header, opt ] = eeg_readoptions( filename, opt_backup );
50
51
    if nargin < 1
52
        help eeg_readoptions;
53
        return;
54
    end
55
    
56
    if nargin < 2
57
        opt_backup = [];
58
    end
59
    
60
    if ischar(filename)
61
         fid = fopen(filename, 'r');
62
    else fid = filename;
63
    end
64
    
65
    % skip header
66
    % -----------
67
    header = '';
68
    str = fgets( fid );
69
    while (str(1) == '%')
70
        header = [ header str];
71
        str = fgets( fid );
72
    end
73
    
74
    % read variables values and description
75
    % --------------------------------------
76
    str = fgetl( fid ); % jump a line
77
    index = 1;
78
    opt = [];
79
    while (str(1) ~= -1)
80
        if str(1) == '%'
81
            opt(index).description = str(3:end-1);
82
            opt(index).value       = [];
83
            opt(index).varname     = '';
84
        else
85
            [ opt(index).varname str ] = strtok(str); % variable name
86
            [ equal              str ] = strtok(str); % =
87
            [ opt(index).value   str ] = strtok(str); % value
88
            [ tmp                str ] = strtok(str); % ;
89
            [ tmp                dsc ] = strtok(str); % comment
90
            dsc = deblank( dsc(end:-1:1) );
91
            opt(index).description = deblank( dsc(end:-1:1) );
92
            opt(index).value       = str2num(  opt(index).value );
93
        end
94
        
95
        str = fgets( fid ); % jump a line
96
        index = index+1;
97
    end
98
    fclose(fid);
99
100
    % replace in backup structure if any
101
    % ----------------------------------
102
    if ~isempty(opt_backup)
103
        if ~isempty(opt)
104
            for index = 1:length(opt_backup)
105
                ind = strmatch(opt_backup(index).varname, { opt.varname }, 'exact');
106
                if ~isempty(ind) && ~isempty(opt_backup(index).varname)
107
                    opt_backup(index).value = opt(ind).value;
108
                end
109
            end
110
        end
111
        opt = opt_backup;
112
    end