link.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function this = link(this, fnamedat, dtype, slope, offset)
  2. % Links the object to data file (only if exists)
  3. % FORMAT this = link(this)
  4. % Will try to find the datafile based on fname and path
  5. % FORMAT this = link(this, fnamedat)
  6. % Will find the datafile using the provided name and path
  7. % FORMAT this = link(this, fnamedat, dtype, slope, offset)
  8. % Additional parameters for non-float data files
  9. % _________________________________________________________________________
  10. % Copyright (C) 2011 Wellcome Trust Centre for Neuroimaging
  11. % Vladimir Litvak
  12. % $Id: link.m 6437 2015-05-14 12:27:21Z vladimir $
  13. if isempty(this)
  14. error('All header dimensions should be >0');
  15. end
  16. if nargin == 1 || isempty(fnamedat)
  17. [p, f] = fileparts(fullfile(this));
  18. fnamedat = fullfile(p, [f '.dat']);
  19. else
  20. [p, f, x] = fileparts(fnamedat);
  21. if isempty(p)
  22. p = path(this);
  23. end
  24. if isempty(x)
  25. x = '.dat';
  26. end
  27. fnamedat = fullfile(p, [f x]);
  28. end
  29. % This is to re-use existing settings for non-floats. Use unlink to clear
  30. % these settings.
  31. if (nargin < 3) && isa(this.data, 'file_array') && ~isequal(lower(this.data.dtype), 'float32-le')
  32. dtype = this.data.dtype;
  33. offset = this.data.offset;
  34. slope = this.data.scl_slope;
  35. else
  36. if nargin < 3, dtype = 'float32-le'; end
  37. if nargin < 4, slope = 1; end
  38. if nargin < 5, offset = 0; end
  39. end
  40. if ~exist(fnamedat, 'file')
  41. error('Data file not found');
  42. end
  43. % Size determination here should worked for unlinked objects and be insensitive
  44. % to online montages.
  45. if ~strncmpi(transformtype(this), 'TF', 2)
  46. siz = [length(this.channels), nsamples(this), ntrials(this)];
  47. else
  48. siz = [length(this.channels), nfrequencies(this), nsamples(this), ntrials(this)];
  49. end
  50. this.data = file_array(fnamedat, siz, dtype, offset, slope);
  51. siz = num2cell(size(this));
  52. try
  53. this.data(siz{:});
  54. catch
  55. error('Dimensions mismatch. Could not link to the data file');
  56. end
  57. this = check(this);