source: prdatasets/pr_getdata.m

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