clone.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function new = clone(this, fnamedat, dim, reset, forcefloat)
  2. % Creates a copy of the object with a new, empty data file,
  3. % possibly changing dimensions
  4. % FORMAT new = clone(this, fnamedat, dim, reset)
  5. % reset - 0 (default) do not reset channel or trial info unless dimensions
  6. % change, 1 - reset channels only, 2 - trials only, 3 both
  7. % forcefloat - force the new data file to be float (0 by default)
  8. % this is to fix an issue with TF analysis of files using int16
  9. % for the raw data
  10. % Note that when fnamedat comes with a path, the cloned meeg object uses
  11. % it. Otherwise, its path is by definition that of the meeg object to be
  12. % cloned.
  13. % _________________________________________________________________________
  14. % Copyright (C) 2008-2012 Wellcome Trust Centre for Neuroimaging
  15. % Stefan Kiebel, Vladimir Litvak
  16. % $Id: clone.m 7449 2018-10-16 13:52:04Z vladimir $
  17. if nargin < 5
  18. forcefloat = 0;
  19. end
  20. if nargin < 4
  21. reset = 0;
  22. end
  23. if nargin < 3
  24. dim = size(this);
  25. end
  26. % if number of channels is modified, throw away montages
  27. if dim(1) ~= nchannels(this)
  28. this = montage(this,'remove',1:montage(this,'getnumber'));
  29. disp('Changing the number of channels, so discarding online montages.');
  30. end
  31. new = montage(this, 'switch', 0);
  32. new = unlink(new);
  33. % check file path first
  34. [pth, fname] = fileparts(fnamedat);
  35. if isempty(pth)
  36. pth = this.path;
  37. end
  38. newFileName = [fullfile(pth, fname),'.dat'];
  39. % copy the file_array
  40. d = this.data; %
  41. d.fname = newFileName;
  42. dim_o = d.dim;
  43. if forcefloat
  44. d.dtype = 'FLOAT32-LE';
  45. d.offset = 0;
  46. d.scl_slope =[];
  47. d.scl_inter =[];
  48. else
  49. % This takes care of an issue specific to int data files which are not
  50. % officially supported in SPM8/12.
  51. if dim(1)>dim_o(1) && length(d.scl_slope)>1
  52. % adding channel to montage and scl_slope defined for old montage
  53. % -> need to increase scl_slope
  54. v_slope = mode(d.scl_slope);
  55. if length(v_slope)>1
  56. warning(['Trying to guess the scaling factor for new channels.',...
  57. ' This might be not exact.']);
  58. end
  59. d.scl_slope = [d.scl_slope' ones(1,dim(1)-dim_o(1))*v_slope]';
  60. end
  61. end
  62. d.dim = dim;
  63. % physically initialise file
  64. initialise(d);
  65. if length(dim) == 3
  66. nsampl = dim(2);
  67. ntrial = dim(3);
  68. new = transformtype(new, 'time');
  69. elseif length(dim) == 4
  70. nsampl = dim(3);
  71. ntrial = dim(4);
  72. if ~strncmpi(transformtype(new), 'TF',2)
  73. new = transformtype(new, 'TF');
  74. end
  75. % This assumes that the frequency axis will be set correctly after
  76. % cloning and is neccesary to avoid an inconsistent state
  77. new.transform.frequencies = 1:dim(2);
  78. else
  79. error('Dimensions different from 3 or 4 are not supported.');
  80. end
  81. % change filenames
  82. new.fname = [fname,'.mat'];
  83. new.path = pth;
  84. % ensure consistency
  85. if (dim(1) ~= nchannels(this)) || ismember(reset, [1 3])
  86. new.channels = [];
  87. for i = 1:dim(1)
  88. new.channels(i).label = ['Ch' num2str(i)];
  89. end
  90. elseif montage(this, 'getindex')
  91. new.channels = [];
  92. lbl = chanlabels(this);
  93. for i = 1:dim(1)
  94. new.channels(i).label = lbl{i};
  95. end
  96. new = chantype(new, ':', chantype(this));
  97. new = units(new, ':', units(this));
  98. new = badchannels(new, ':', badchannels(this));
  99. new = coor2D(new, ':', coor2D(this));
  100. end
  101. if ntrial ~= ntrials(this) || ismember(reset, [2 3])
  102. new.trials = repmat(struct('label', 'Undefined'), 1, ntrial);
  103. end
  104. if (nsampl ~= nsamples(this))
  105. new.Nsamples = nsampl;
  106. end
  107. new = check(new);
  108. % link into new meeg object
  109. new = link(new, d.fname, d.dtype, d.scl_slope, d.offset);
  110. if strncmpi(transformtype(new),'TF',2) && strncmpi(transformtype(this),'TF',2) ...
  111. && (nfrequencies(new) == nfrequencies(this))
  112. new = frequencies(new, ':', frequencies(this));
  113. end
  114. save(new);