source: prdatasets/pr_getdata.m @ 141

Last change on this file since 141 was 140, checked in by bduin, 5 years ago
File size: 4.0 KB
RevLine 
[123]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%
[137]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.
[123]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%
[137]23% SEE ALSO (<a href="http://37steps.com/prhtml/37tools.html">37tools Contents</a>)
24% DATASETS, DATAFILES
[123]25
[137]26% Copyright: R.P.W. Duin, r.p.w.duin@37steps.com
[123]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
[137]37name = pr_callername;
[140]38argin = setdefaults(varargin,[],[],[],[],[]);
[123]39[url,size,dset,field,ask] = deal(argin{:});
[140]40if isempty(ask)
41  if isempty(size)
42    ask = false;
43  else
44    ask = true;
45  end
46end
[137]47
48if isempty(url)
49  url = ['http://prtools.tudelft.nl/prdatasets/' name '.mat'];
50end
[123]51[dummy,uname,ext] = fileparts(url);
52
53if isempty(name)
54  ddir = pwd;
55else
[137]56  ddir = fullfile(fileparts(which(name)),'data');
[123]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
[140]71    ask_download(name,size)
[123]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
93else
94  a = dset;
95end
96 
[140]97function ask_download(name,size)
[123]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
[140]110    q = input(['Dataset ' name ' is not available, OK to download ' siz ' [y]/n ?'],'s');
[123]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[dummy,dummy,ext] = fileparts(dset);
123if isempty(ext)
124  dsetmat = [dset '.mat'];
125else
126  dsetmat = dset;
127end
128
129if exist([dsetmat],'file') == 2
130  try
131    s = prload(dsetmat);
[137]132  catch ME
[123]133    out = 'error';
134    return
135  end
136  if isstruct(s)
137    if isempty(field)
138      f = fieldnames(s);
139      out = getfield(s,f{1});
140    else
141      out = getfield(s,field);
142    end
143  else
144    out = s;
145  end
146elseif exist(dset,'dir') == 7
147  [dummy,dfile] = fileparts(dset);
148  if exist(fullfile(dset,[dfile '.mat']),'file') == 2
149    out = prdatafile(dset);
150  elseif ~isempty(field)
151    % field is now datafile type
152    out = prdatafile(dset,field);
153  else
154    out = 'dir';
155  end
156end
157
158
159
160
Note: See TracBrowser for help on using the repository browser.