source: prdatasets/pr_getdata.m @ 142

Last change on this file since 142 was 142, checked in by bduin, 5 years ago

Updated collection of datasets

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