File: CalDataFolder.m

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (124 lines) | stat: -rwxr-xr-x 4,373 bytes parent folder | download | duplicates (3)
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
function directory=CalDataFolder(forceDemo,calFileName,calDir)
% directory=CalDataFolder([forceDemo],[calFileName],[calDir])
%
% Get the path to the CalData folder.
%
% If "forceDemo" is true (false by default), then force use of
% PsychCalDemoData.  Otherwise return location of PsychCalLocalData
% if it exists, and PsychCalDemoData if it PsychCalLocalData does
% not exist.
%
% If calFileName is passed, it checks if the specified file exists
% in the top level of the directory it located.  If so, it returns
% that directory.  If not, it searches the first level subdirectories
% of the top level directories and if the specfied file is in one of
% them, returns that.  Skips subdirs called 'xOld', 'Plots', and those
% that begin with '.'.
%
% calFileName can either have .mat postpended or not.
%
% If calDir is passed as a string, that is used as the directory, with
% the subdir searching as above.
%
% See also LoadCalFile, SaveCalFile.
%
% Denis Pelli 7/25/96
% Denis Pelli 2/28/98 change "CalDat" to "PsychCalData"
% 8/14/00  dhb  Add alternate name, change names. 
% 4/1/07   dhb  Fix subtle bug in error message when there are duplicate cal
%               folders on path. 
% 3/7/08   mpr  changed documentation to make it consistent (apparently
%               "forceDemo" used to be "alt"
% 4/2/13   dhb  Add calFileName and associated behavior.
% 6/2/13   dhb  Make this properly return subfolder containing calibration file
%               if .mat is not postpended.
% 6/10/13  dhb  Fix buglet introduced 6/2/13 -- need to handle empty calFileName (thanks to MS for
%               identifying the problem and the fix.
% 3/29/16  dhb  Allow multiple PsychCalDemo data on path, but issue a warning printout.

% Set forceDemo flag
if (nargin < 1 || isempty(forceDemo))
	forceDemo = 0;
end
if (nargin < 2 || isempty(calFileName))
    calFileName = [];
end

% Postpend .mat if necessary
if (~isempty(calFileName) && ~strcmp(calFileName(end-3:end),'.mat'))
    calFileName = [calFileName '.mat'];
end

% If dir is passed we just use that.  Otherwise
% do our thing.
if (nargin < 3 || isempty(calDir)) 
    name='PsychCalLocalData';
    alternateName ='PsychCalDemoData';
    
    % Find name.  If not there, find alternate
    if (~forceDemo)
        directory = FindFolder(name);
        duplicateMsgName = name;
    else
        directory = [];
    end
    if isempty(directory)
        directory=FindFolder(alternateName);
        duplicateMsgName = alternateName;
    end
    
    % If both finds fail, print out error message.  This
    % should never happen as we put 'PsychCalDemoData' in
    % the toolbox distribution.
    if isempty(directory)
        error(['Can''t find any ''' name ''' or ''' alternateName '''folders in the Matlab path.']);
    end
    
    % If we found multiple copies of a calibration folder, we warn.
    % This also should never happen.
    if size(directory,1)>1
        for i=1:size(directory,1)
            disp(['DUPLICATE: ''' deblank(directory(i,:)) '''']);
        end
       fprintf(['Warning: found more than one ''' duplicateMsgName ''' folder in the Matlab path.']);
       directory = deblank(directory(1,:));
    end
else
    directory = calDir;
end

% If we were passed a calibration file name, figure out whether it exists in
% the found directory, or a subfolder of the directory.
if (~isempty(calFileName))
    curDir = pwd;
    cd(directory);
    
    % Is the file we're after here?  If so, we're good
    % and we just return after restoring directory.
    % Otherwise, we look in any subdirectories other
    % than those beginning with 'xOld' or 'Plot' and
    % see if our baby is there.
    foundOne = 0;
    dirRet = dir([calFileName]);
    if (~isempty(dirRet))
        foundOne = 1;
    end
    allDirs = dir;
    for i = 1:length(allDirs)
        if (allDirs(i).isdir && ~strcmp(allDirs(i).name,'xOld') && ~strcmp(allDirs(i).name,'Plots') && allDirs(i).name(1) ~= '.')
            cd(allDirs(i).name);
            dirRet = dir(calFileName);
            cd('..');
            if (~isempty(dirRet))
                if (foundOne)
                    error('More than one copy of the desired calibration file found');
                end
                directory = [directory allDirs(i).name filesep];
                foundOne = 1;
            end
        end
    end
    
    cd(curDir);
end