[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.
|
---|
[151] | 13 | % If available, the dataset DSET stored in FIELD is returned in OUT. If
|
---|
| 14 | % FIELD is not set all fields are returned in a cell array.
|
---|
| 15 | %
|
---|
| 16 | % In case of a datafile DFILE, it is created, if necessary, with given
|
---|
[137] | 17 | % TYPE and returned in OUT. In case the download was successful but no
|
---|
| 18 | % dataset or datfile could be created (e.g. because of empty TYPE) OUT is
|
---|
| 19 | % empty, otherwise an error is generated.
|
---|
[123] | 20 | %
|
---|
| 21 | % This is a low-level routine, typically used in COMMAND and not called
|
---|
| 22 | % from the command line. COMMAND should take care that a proper dataset
|
---|
| 23 | % is constucted and returned to the user.
|
---|
| 24 | %
|
---|
[150] | 25 | % SEE ALSO
|
---|
[137] | 26 | % DATASETS, DATAFILES
|
---|
[123] | 27 |
|
---|
[150] | 28 | % Copyright: R.P.W. Duin
|
---|
[123] | 29 |
|
---|
| 30 | function out = pr_getdata(varargin)
|
---|
| 31 |
|
---|
| 32 | % name of calling routine, might be used for the dataset
|
---|
| 33 | %
|
---|
| 34 | % name : name of calling routine, might be used for the dataset
|
---|
| 35 | % url : url of dataset
|
---|
| 36 | % uname : dataset name as used in url
|
---|
| 37 | % dset : becomes full path and name of dataset
|
---|
| 38 | % ddir : becomes full path of dataset
|
---|
[137] | 39 | name = pr_callername;
|
---|
[140] | 40 | argin = setdefaults(varargin,[],[],[],[],[]);
|
---|
[123] | 41 | [url,size,dset,field,ask] = deal(argin{:});
|
---|
[140] | 42 | if isempty(ask)
|
---|
| 43 | if isempty(size)
|
---|
| 44 | ask = false;
|
---|
| 45 | else
|
---|
| 46 | ask = true;
|
---|
| 47 | end
|
---|
| 48 | end
|
---|
[137] | 49 |
|
---|
| 50 | if isempty(url)
|
---|
| 51 | url = ['http://prtools.tudelft.nl/prdatasets/' name '.mat'];
|
---|
| 52 | end
|
---|
[142] | 53 | [~,uname,ext] = fileparts(url);
|
---|
[123] | 54 |
|
---|
| 55 | if isempty(name)
|
---|
| 56 | ddir = pwd;
|
---|
| 57 | else
|
---|
[137] | 58 | ddir = fullfile(fileparts(which(name)),'data');
|
---|
[123] | 59 | end
|
---|
| 60 |
|
---|
| 61 | if isempty(dset)
|
---|
| 62 | if isempty(name)
|
---|
| 63 | dset = [uname ext];
|
---|
| 64 | else
|
---|
| 65 | dset = [name ext];
|
---|
| 66 | end
|
---|
| 67 | end
|
---|
| 68 | dset = fullfile(ddir,dset);
|
---|
| 69 |
|
---|
| 70 | out = tryload(dset,field);
|
---|
| 71 | if isempty(out)
|
---|
| 72 | if ask
|
---|
[140] | 73 | ask_download(name,size)
|
---|
[123] | 74 | end
|
---|
| 75 | % download in dir of dset
|
---|
| 76 | status = prdownload(url,fileparts(dset));
|
---|
| 77 | % get naming consistent, avoid Matlab naming problems with capitals
|
---|
| 78 | if ~any(strcmp(ext,{'.zip','.gz','.tgz','.tar'}))
|
---|
| 79 | % compressed files are already deleted
|
---|
| 80 | movefile([fullfile(fileparts(dset),uname) ext],[dset 'temp']);
|
---|
| 81 | movefile([dset 'temp'],dset);
|
---|
| 82 | end
|
---|
| 83 | if status
|
---|
| 84 | out = tryload(dset,field);
|
---|
| 85 | else
|
---|
| 86 | error('Download failed')
|
---|
| 87 | end
|
---|
| 88 | elseif strcmp(out,'dir')
|
---|
| 89 | if strcmp(field,'raw')
|
---|
| 90 | % we have a datafile!!
|
---|
| 91 | out = prdatafile(dset);
|
---|
| 92 | else
|
---|
| 93 | out = [];
|
---|
| 94 | end
|
---|
| 95 | end
|
---|
| 96 |
|
---|
[140] | 97 | function ask_download(name,size)
|
---|
[123] | 98 |
|
---|
| 99 | global ASK
|
---|
| 100 | if isempty(ASK)
|
---|
| 101 | ASK = true;
|
---|
| 102 | end
|
---|
| 103 |
|
---|
| 104 | if ASK
|
---|
| 105 | if ~isempty(size)
|
---|
| 106 | siz = ['(' num2str(size) ' MB)'];
|
---|
| 107 | else
|
---|
| 108 | siz = '';
|
---|
| 109 | end
|
---|
[140] | 110 | q = input(['Dataset ' name ' is not available, OK to download ' siz ' [y]/n ?'],'s');
|
---|
[123] | 111 | if ~isempty(q) && ~strcmp(q,'y')
|
---|
| 112 | error('Dataset not found')
|
---|
| 113 | end
|
---|
| 114 | end
|
---|
| 115 |
|
---|
| 116 | return
|
---|
| 117 |
|
---|
| 118 | function out = tryload(dset,field)
|
---|
| 119 | out = [];
|
---|
| 120 |
|
---|
| 121 | % make sure we check for a matfile
|
---|
[142] | 122 | [~,~,ext] = fileparts(dset);
|
---|
[123] | 123 | if isempty(ext)
|
---|
| 124 | dsetmat = [dset '.mat'];
|
---|
| 125 | else
|
---|
| 126 | dsetmat = dset;
|
---|
| 127 | end
|
---|
| 128 |
|
---|
| 129 | if exist([dsetmat],'file') == 2
|
---|
| 130 | try
|
---|
[151] | 131 | out = file2dset(dsetmat);
|
---|
[137] | 132 | catch ME
|
---|
[151] | 133 | s = load(dsetmat);
|
---|
| 134 | if isstruct(s)
|
---|
| 135 | if isempty(field)
|
---|
| 136 | s = struct2cell(s);
|
---|
| 137 | out = s{1};
|
---|
| 138 | else
|
---|
| 139 | out = getfield(s,field);
|
---|
| 140 | end
|
---|
[123] | 141 | else
|
---|
[151] | 142 | out = s;
|
---|
[123] | 143 | end
|
---|
| 144 | end
|
---|
| 145 | elseif exist(dset,'dir') == 7
|
---|
[142] | 146 | [~,dfile] = fileparts(dset);
|
---|
[123] | 147 | if exist(fullfile(dset,[dfile '.mat']),'file') == 2
|
---|
| 148 | out = prdatafile(dset);
|
---|
| 149 | elseif ~isempty(field)
|
---|
| 150 | % field is now datafile type
|
---|
| 151 | out = prdatafile(dset,field);
|
---|
| 152 | else
|
---|
| 153 | out = 'dir';
|
---|
| 154 | end
|
---|
| 155 | end
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|