source: prdatasets/pr_getdata.m @ 137

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