|
22 | 22 | % mtVariable = MappedTensor(..., 'Class', strClassName) |
23 | 23 | % mtVariable = MappedTensor(..., 'HeaderBytes', nHeaderBytesToSkip) |
24 | 24 | % mtVariable = MappedTensor(..., 'MachineFormat', strMachineFormat) |
| 25 | +% mtVariable = MappedTensor(tExistingTensor, 'Convert') |
| 26 | +% mtVariable = MappedTensor(..., 'Like', tExistingTensor) |
25 | 27 | % |
26 | 28 | % 'vnTensorSize', or [nDim1 nDim2 nDim3 ...] defines the desired size of the |
27 | 29 | % variable. By default, a new binary temporary file will be generated, and |
|
47 | 49 | % ('ieee-be') or little-endian ('ieee-le') formats for data storage and |
48 | 50 | % reading. If not specified, the machine-native format will be used. |
49 | 51 | % |
| 52 | +% The optional argument 'Convert' allows you to convert an existing matlab |
| 53 | +% tensor 'tExistingTensor' into a MappedTensor, of the appropriate class. |
| 54 | +% |
| 55 | +% The optional argument 'Like' allows you to create a MappedTensor with the |
| 56 | +% same class and complexity (i.e. real and complex) of 'tExistingTensor'. |
| 57 | +% Note that sparse MappedTensors are not supported. |
| 58 | +% |
50 | 59 | % Usage: size(mtVariable) |
51 | 60 | % mtVariable(:) = rand(100, 100, 100); |
52 | 61 | % mfData = mtVariable(:, :, 34, 2); |
|
215 | 224 | vbKeepArg(nArg:nArg+1) = false; |
216 | 225 | nArg = nArg + 1; |
217 | 226 |
|
| 227 | + case {'convert'} |
| 228 | + % - Convert an existing tensor into a MappedTensor |
| 229 | + if (nargin > 2) |
| 230 | + error('MappedTensor:Usage', ... |
| 231 | + '*** MappedTensor: Only a single input argument is required when using ''Convert''.'); |
| 232 | + end |
| 233 | + |
| 234 | + % - Do we already have a MappedTensor? |
| 235 | + if (isa(varargin{1}, 'MappedTensor')) |
| 236 | + % - Just return it |
| 237 | + mtVar = varargin{1}; |
| 238 | + return; |
| 239 | + |
| 240 | + else |
| 241 | + % - Get the |
| 242 | + tfSourceTensor = varargin{1}; |
| 243 | + |
| 244 | + % - Check the size of the incoming tensor |
| 245 | + if (numel(tfSourceTensor) == 0) |
| 246 | + error('MappedTensor:Arguments', ... |
| 247 | + '*** MappedTensor: A zero-sized tensor cannot be converted to a MappedTensor.'); |
| 248 | + end |
| 249 | + |
| 250 | + % - Remove 'Convert' arguments from varargin |
| 251 | + varargin([1 nArg]) = []; |
| 252 | + |
| 253 | + % - Create a MappedTensor |
| 254 | + mtVar = MappedTensor(size(tfSourceTensor), varargin{:}, 'Like', tfSourceTensor); |
| 255 | + |
| 256 | + % - Copy the data |
| 257 | + subsasgn(mtVar, substruct('()', {':'}), tfSourceTensor); |
| 258 | + return; |
| 259 | + end |
| 260 | + |
| 261 | + case {'like'} |
| 262 | + % - Set the class property accordingly |
| 263 | + mtVar.strClass = class(varargin{nArg+1}); |
| 264 | + |
| 265 | + % - Set the complexity (real or complex) accordingly |
| 266 | + if (~isreal(varargin{nArg+1})) |
| 267 | + mtVar.bIsComplex = true; |
| 268 | + end |
| 269 | + |
| 270 | + vbKeepArg(nArg:nArg+1) = false; |
| 271 | + nArg = nArg + 1; |
| 272 | + |
218 | 273 | otherwise |
219 | 274 | % - No other properties are supported |
220 | 275 | error('MappedTensor:InvalidProperty', ... |
|
305 | 360 |
|
306 | 361 | % - Record number of total elements |
307 | 362 | mtVar.nNumElements = prod(vnTensorSize); |
| 363 | + |
| 364 | + % - Set complexity |
| 365 | + if (mtVar.bIsComplex) |
| 366 | + make_complex(mtVar); |
| 367 | + end |
308 | 368 | end |
309 | 369 |
|
310 | 370 | % delete - DESTRUCTOR |
|
0 commit comments