[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
|
---|
[142] | 9 | % be downloaded. If ASK = true, the user is asked for approval.
|
---|
[123] | 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 |
|
---|
| 28 | function 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] | 37 | name = pr_callername;
|
---|
[140] | 38 | argin = setdefaults(varargin,[],[],[],[],[]);
|
---|
[123] | 39 | [url,size,dset,field,ask] = deal(argin{:});
|
---|
[140] | 40 | if isempty(ask)
|
---|
| 41 | if isempty(size)
|
---|
| 42 | ask = false;
|
---|
| 43 | else
|
---|
| 44 | ask = true;
|
---|
| 45 | end
|
---|
| 46 | end
|
---|
[137] | 47 |
|
---|
| 48 | if isempty(url)
|
---|
| 49 | url = ['http://prtools.tudelft.nl/prdatasets/' name '.mat'];
|
---|
| 50 | end
|
---|
[142] | 51 | [~,uname,ext] = fileparts(url);
|
---|
[123] | 52 |
|
---|
| 53 | if isempty(name)
|
---|
| 54 | ddir = pwd;
|
---|
| 55 | else
|
---|
[137] | 56 | ddir = fullfile(fileparts(which(name)),'data');
|
---|
[123] | 57 | end
|
---|
| 58 |
|
---|
| 59 | if isempty(dset)
|
---|
| 60 | if isempty(name)
|
---|
| 61 | dset = [uname ext];
|
---|
| 62 | else
|
---|
| 63 | dset = [name ext];
|
---|
| 64 | end
|
---|
| 65 | end
|
---|
| 66 | dset = fullfile(ddir,dset);
|
---|
| 67 |
|
---|
| 68 | out = tryload(dset,field);
|
---|
| 69 | if 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
|
---|
| 86 | elseif strcmp(out,'dir')
|
---|
| 87 | if strcmp(field,'raw')
|
---|
| 88 | % we have a datafile!!
|
---|
| 89 | out = prdatafile(dset);
|
---|
| 90 | else
|
---|
| 91 | out = [];
|
---|
| 92 | end
|
---|
| 93 | end
|
---|
| 94 |
|
---|
[140] | 95 | function ask_download(name,size)
|
---|
[123] | 96 |
|
---|
| 97 | global ASK
|
---|
| 98 | if isempty(ASK)
|
---|
| 99 | ASK = true;
|
---|
| 100 | end
|
---|
| 101 |
|
---|
| 102 | if ASK
|
---|
| 103 | if ~isempty(size)
|
---|
| 104 | siz = ['(' num2str(size) ' MB)'];
|
---|
| 105 | else
|
---|
| 106 | siz = '';
|
---|
| 107 | end
|
---|
[140] | 108 | q = input(['Dataset ' name ' is not available, OK to download ' siz ' [y]/n ?'],'s');
|
---|
[123] | 109 | if ~isempty(q) && ~strcmp(q,'y')
|
---|
| 110 | error('Dataset not found')
|
---|
| 111 | end
|
---|
| 112 | end
|
---|
| 113 |
|
---|
| 114 | return
|
---|
| 115 |
|
---|
| 116 | function out = tryload(dset,field)
|
---|
| 117 | out = [];
|
---|
| 118 |
|
---|
| 119 | % make sure we check for a matfile
|
---|
[142] | 120 | [~,~,ext] = fileparts(dset);
|
---|
[123] | 121 | if isempty(ext)
|
---|
| 122 | dsetmat = [dset '.mat'];
|
---|
| 123 | else
|
---|
| 124 | dsetmat = dset;
|
---|
| 125 | end
|
---|
| 126 |
|
---|
| 127 | if exist([dsetmat],'file') == 2
|
---|
| 128 | try
|
---|
| 129 | s = prload(dsetmat);
|
---|
[137] | 130 | catch ME
|
---|
[123] | 131 | out = 'error';
|
---|
| 132 | return
|
---|
| 133 | end
|
---|
| 134 | if isstruct(s)
|
---|
| 135 | if isempty(field)
|
---|
| 136 | f = fieldnames(s);
|
---|
| 137 | out = getfield(s,f{1});
|
---|
| 138 | else
|
---|
| 139 | out = getfield(s,field);
|
---|
| 140 | end
|
---|
| 141 | else
|
---|
| 142 | out = s;
|
---|
| 143 | end
|
---|
| 144 | elseif exist(dset,'dir') == 7
|
---|
[142] | 145 | [~,dfile] = fileparts(dset);
|
---|
[123] | 146 | if exist(fullfile(dset,[dfile '.mat']),'file') == 2
|
---|
| 147 | out = prdatafile(dset);
|
---|
| 148 | elseif ~isempty(field)
|
---|
| 149 | % field is now datafile type
|
---|
| 150 | out = prdatafile(dset,field);
|
---|
| 151 | else
|
---|
| 152 | out = 'dir';
|
---|
| 153 | end
|
---|
| 154 | end
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 |
|
---|