%PR_GETDATA Loads PRTOOLS dataset for any toolbox (mat-files) % % OUT = PR_GETDATA(URL,SIZE,DSET,FIELD,ASK) % OUT = PR_GETDATA(URL,SIZE,DFILE,TYPE,ASK) % % Checks the availability of the PRTools dataset DSET or datafile DFILE. % By default DSET is COMMAND.mat with COMMAND the name of the calling % m-file. If this is not available in the directory of COMMAND the URL will % be downloaded. If ASK = true, the user is asked for approval. % If given, SIZE (in MByte) is displayed in the request. % % A mat-file returns a structure with fields pointing to stored variables. % If available, the dataset DSET stored in FIELD (default 1) is returned in % OUT. In case of a datafile DFILE, it is created, if necessary, with given % TYPE and returned in OUT. In case the download was successful but no % dataset or datfile could be created (e.g. because of empty TYPE) OUT is % empty, otherwise an error is generated. % % This is a low-level routine, typically used in COMMAND and not called % from the command line. COMMAND should take care that a proper dataset % is constucted and returned to the user. % % SEE ALSO (37tools Contents) % DATASETS, DATAFILES % Copyright: R.P.W. Duin, r.p.w.duin@37steps.com function out = pr_getdata(varargin) % name of calling routine, might be used for the dataset % % name : name of calling routine, might be used for the dataset % url : url of dataset % uname : dataset name as used in url % dset : becomes full path and name of dataset % ddir : becomes full path of dataset name = pr_callername; argin = setdefaults(varargin,[],[],[],[],[]); [url,size,dset,field,ask] = deal(argin{:}); if isempty(ask) if isempty(size) ask = false; else ask = true; end end if isempty(url) url = ['http://prtools.tudelft.nl/prdatasets/' name '.mat']; end [~,uname,ext] = fileparts(url); if isempty(name) ddir = pwd; else ddir = fullfile(fileparts(which(name)),'data'); end if isempty(dset) if isempty(name) dset = [uname ext]; else dset = [name ext]; end end dset = fullfile(ddir,dset); out = tryload(dset,field); if isempty(out) if ask ask_download(name,size) end % download in dir of dset status = prdownload(url,fileparts(dset)); % get naming consistent, avoid Matlab naming problems with capitals if ~any(strcmp(ext,{'.zip','.gz','.tgz','.tar'})) % compressed files are already deleted movefile([fullfile(fileparts(dset),uname) ext],[dset 'temp']); movefile([dset 'temp'],dset); end if status out = tryload(dset,field); else error('Download failed') end elseif strcmp(out,'dir') if strcmp(field,'raw') % we have a datafile!! out = prdatafile(dset); else out = []; end end function ask_download(name,size) global ASK if isempty(ASK) ASK = true; end if ASK if ~isempty(size) siz = ['(' num2str(size) ' MB)']; else siz = ''; end q = input(['Dataset ' name ' is not available, OK to download ' siz ' [y]/n ?'],'s'); if ~isempty(q) && ~strcmp(q,'y') error('Dataset not found') end end return function out = tryload(dset,field) out = []; % make sure we check for a matfile [~,~,ext] = fileparts(dset); if isempty(ext) dsetmat = [dset '.mat']; else dsetmat = dset; end if exist([dsetmat],'file') == 2 try s = prload(dsetmat); catch ME out = 'error'; return end if isstruct(s) if isempty(field) f = fieldnames(s); out = getfield(s,f{1}); else out = getfield(s,field); end else out = s; end elseif exist(dset,'dir') == 7 [~,dfile] = fileparts(dset); if exist(fullfile(dset,[dfile '.mat']),'file') == 2 out = prdatafile(dset); elseif ~isempty(field) % field is now datafile type out = prdatafile(dset,field); else out = 'dir'; end end