source: prdatasets/pr_getdata.m @ 135

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