Skip to content

Commit 1b4c506

Browse files
committed
* Added new constructor options 'Convert' and 'Like'. 'Convert' converts an existing matlab tensor into a MappedTensor. 'Like' creates a new mapped tensor with the class and complexity set by an existing matlab tensor.
1 parent cc757cc commit 1b4c506

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

MappedTensor.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
% mtVariable = MappedTensor(..., 'Class', strClassName)
2323
% mtVariable = MappedTensor(..., 'HeaderBytes', nHeaderBytesToSkip)
2424
% mtVariable = MappedTensor(..., 'MachineFormat', strMachineFormat)
25+
% mtVariable = MappedTensor(tExistingTensor, 'Convert')
26+
% mtVariable = MappedTensor(..., 'Like', tExistingTensor)
2527
%
2628
% 'vnTensorSize', or [nDim1 nDim2 nDim3 ...] defines the desired size of the
2729
% variable. By default, a new binary temporary file will be generated, and
@@ -47,6 +49,13 @@
4749
% ('ieee-be') or little-endian ('ieee-le') formats for data storage and
4850
% reading. If not specified, the machine-native format will be used.
4951
%
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+
%
5059
% Usage: size(mtVariable)
5160
% mtVariable(:) = rand(100, 100, 100);
5261
% mfData = mtVariable(:, :, 34, 2);
@@ -215,6 +224,52 @@
215224
vbKeepArg(nArg:nArg+1) = false;
216225
nArg = nArg + 1;
217226

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+
218273
otherwise
219274
% - No other properties are supported
220275
error('MappedTensor:InvalidProperty', ...
@@ -305,6 +360,11 @@
305360

306361
% - Record number of total elements
307362
mtVar.nNumElements = prod(vnTensorSize);
363+
364+
% - Set complexity
365+
if (mtVar.bIsComplex)
366+
make_complex(mtVar);
367+
end
308368
end
309369

310370
% delete - DESTRUCTOR

0 commit comments

Comments
 (0)