source: prdatasets/pr_getdata.m @ 126

Last change on this file since 126 was 123, checked in by bduin, 7 years ago
File size: 3.8 KB
Line 
1%PR_GETDATA Loads PRTOOLS dataset for any toolbox (mat-files)
2%
3%               OUT = PR_GETDATA(URL,SIZE,DSET,FIELD,ASK)
4%               OUT = PR_GETDATA(URL,SIZE,DFILE,TYPE,ASK)
5%
6% Checks the availability of the PRTools dataset DSET or datafile DFILE.
7% By default DSET is COMMAND.mat with COMMAND the name of the calling
8% m-file. If this is not available in the directory of COMMAND the URL will
9% be downloaded. If ASK = true (default), the user is asked for approval.
10% If given, SIZE (in MByte) is displayed in the request.
11%
12% If available, the dataset DSET stored in FIELD is returned in OUT.
13% In case of a datafile DFILE, it is created, if necessary, with given TYPE
14% and returned in OUT. In case the download was successful but no dataset
15% or datfile could be created (e.g. because of empty TYPE) OUT is empty,
16% otherwise an error is generated.
17%
18% This is a low-level routine, typically used in COMMAND and not called
19% from the command line. COMMAND should take care that a proper dataset
20% is constucted and returned to the user.
21%
22% SEE ALSO (<a href="http://37steps.com/prhtml/37tools.html">37tools Contents</a>)
23% DATASETS, DATAFILES
24
25% Copyright: R.P.W. Duin, r.p.w.duin@37steps.com
26
27function out = pr_getdata(varargin)
28
29% name of calling routine, might be used for the dataset
30%
31%  name  : name of calling routine, might be used for the dataset
32%  url   : url of dataset
33%  uname : dataset name as used in url
34%  dset  : becomes full path and name of dataset
35%  ddir  : becomes full path of dataset
36name = callername;
37argin = setdefaults(varargin,[],[],[],[],true);
38[url,size,dset,field,ask] = deal(argin{:});
39[dummy,uname,ext] = fileparts(url);
40
41if isempty(name)
42  ddir = pwd;
43else
44  ddir = fileparts(which(name));
45end
46
47if isempty(dset)
48  if isempty(name)
49    dset = [uname ext];
50  else
51    dset = [name ext];
52  end
53end
54dset = fullfile(ddir,dset);
55
56out = tryload(dset,field);
57if isempty(out)
58  if ask
59    ask_download(size)
60  end
61  % download in dir of dset
62  status = prdownload(url,fileparts(dset));
63  % get naming consistent, avoid Matlab naming problems with capitals
64  if ~any(strcmp(ext,{'.zip','.gz','.tgz','.tar'}))
65    % compressed files are already deleted
66    movefile([fullfile(fileparts(dset),uname) ext],[dset 'temp']);
67    movefile([dset 'temp'],dset);
68  end
69  if status
70    out = tryload(dset,field);
71  else
72    error('Download failed')
73  end
74elseif strcmp(out,'dir')
75  if strcmp(field,'raw')
76    % we have a datafile!!
77    out = prdatafile(dset);
78  else
79    out = [];
80  end
81else
82  a = dset;
83end
84 
85function ask_download(size)
86
87  global ASK
88  if isempty(ASK)
89    ASK = true;
90  end
91 
92  if ASK
93    if ~isempty(size)
94      siz = ['(' num2str(size) ' MB)'];
95    else
96      siz = '';
97    end
98    q = input(['Dataset is not available, OK to download ' siz ' [y]/n ?'],'s');
99    if ~isempty(q) && ~strcmp(q,'y')
100      error('Dataset not found')
101    end
102  end
103 
104return
105
106function out = tryload(dset,field)
107out = [];
108
109% make sure we check for a matfile
110[dummy,dummy,ext] = fileparts(dset);
111if isempty(ext)
112  dsetmat = [dset '.mat'];
113else
114  dsetmat = dset;
115end
116
117if exist([dsetmat],'file') == 2
118  try
119    s = prload(dsetmat);
120  catch
121    out = 'error';
122    return
123  end
124  if isstruct(s)
125    if isempty(field)
126      f = fieldnames(s);
127      out = getfield(s,f{1});
128    else
129      out = getfield(s,field);
130    end
131  else
132    out = s;
133  end
134elseif exist(dset,'dir') == 7
135  [dummy,dfile] = fileparts(dset);
136  if exist(fullfile(dset,[dfile '.mat']),'file') == 2
137    out = prdatafile(dset);
138  elseif ~isempty(field)
139    % field is now datafile type
140    out = prdatafile(dset,field);
141  else
142    out = 'dir';
143  end
144end
145
146function name = callername
147[ss,dummy] = dbstack;
148if length(ss) < 3
149        name = [];
150else
151        name = ss(3).name;
152end
153
154
155
Note: See TracBrowser for help on using the repository browser.