source: prdatasets/pr_getdata.m @ 129

Last change on this file since 129 was 129, checked in by bduin, 6 years ago
File size: 3.8 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%
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
[129]36
[123]37name = callername;
38argin = setdefaults(varargin,[],[],[],[],true);
39[url,size,dset,field,ask] = deal(argin{:});
40[dummy,uname,ext] = fileparts(url);
41
42if isempty(name)
43  ddir = pwd;
44else
45  ddir = fileparts(which(name));
46end
47
48if isempty(dset)
49  if isempty(name)
50    dset = [uname ext];
51  else
52    dset = [name ext];
53  end
54end
55dset = fullfile(ddir,dset);
56
57out = tryload(dset,field);
58if isempty(out)
59  if ask
60    ask_download(size)
61  end
62  % download in dir of dset
63  status = prdownload(url,fileparts(dset));
64  % get naming consistent, avoid Matlab naming problems with capitals
65  if ~any(strcmp(ext,{'.zip','.gz','.tgz','.tar'}))
66    % compressed files are already deleted
67    movefile([fullfile(fileparts(dset),uname) ext],[dset 'temp']);
68    movefile([dset 'temp'],dset);
69  end
70  if status
71    out = tryload(dset,field);
72  else
73    error('Download failed')
74  end
75elseif strcmp(out,'dir')
76  if strcmp(field,'raw')
77    % we have a datafile!!
78    out = prdatafile(dset);
79  else
80    out = [];
81  end
82else
83  a = dset;
84end
85 
86function ask_download(size)
87
88  global ASK
89  if isempty(ASK)
90    ASK = true;
91  end
92 
93  if ASK
94    if ~isempty(size)
95      siz = ['(' num2str(size) ' MB)'];
96    else
97      siz = '';
98    end
99    q = input(['Dataset is not available, OK to download ' siz ' [y]/n ?'],'s');
100    if ~isempty(q) && ~strcmp(q,'y')
101      error('Dataset not found')
102    end
103  end
104 
105return
106
107function out = tryload(dset,field)
108out = [];
109
110% make sure we check for a matfile
111[dummy,dummy,ext] = fileparts(dset);
112if isempty(ext)
113  dsetmat = [dset '.mat'];
114else
115  dsetmat = dset;
116end
117
118if exist([dsetmat],'file') == 2
119  try
120    s = prload(dsetmat);
121  catch
122    out = 'error';
123    return
124  end
125  if isstruct(s)
126    if isempty(field)
127      f = fieldnames(s);
128      out = getfield(s,f{1});
129    else
130      out = getfield(s,field);
131    end
132  else
133    out = s;
134  end
135elseif exist(dset,'dir') == 7
136  [dummy,dfile] = fileparts(dset);
137  if exist(fullfile(dset,[dfile '.mat']),'file') == 2
138    out = prdatafile(dset);
139  elseif ~isempty(field)
140    % field is now datafile type
141    out = prdatafile(dset,field);
142  else
143    out = 'dir';
144  end
145end
146
147function name = callername
148[ss,dummy] = dbstack;
149if length(ss) < 3
150        name = [];
151else
152        name = ss(3).name;
153end
154
155
156
Note: See TracBrowser for help on using the repository browser.