spm_eeg_load.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function D = spm_eeg_load(P)
  2. % Load an M/EEG file in SPM format
  3. % FORMAT D = spm_eeg_load(P)
  4. %
  5. % P - filename of M/EEG file
  6. % D - MEEG object
  7. %__________________________________________________________________________
  8. %
  9. % spm_eeg_load loads an M/EEG file using the SPM MEEG format. Importantly,
  10. % the data array is memory-mapped and the struct is converted to MEEG object.
  11. %__________________________________________________________________________
  12. % Copyright (C) 2008-2012 Wellcome Trust Centre for Neuroimaging
  13. % Stefan Kiebel
  14. % $Id: spm_eeg_load.m 6102 2014-07-14 09:19:09Z vladimir $
  15. %-Bypass if the input is already an MEEG object
  16. %--------------------------------------------------------------------------
  17. if nargin && isa(P, 'meeg')
  18. D = P;
  19. return;
  20. end
  21. %-Get filename
  22. %--------------------------------------------------------------------------
  23. if ~nargin
  24. [P, sts] = spm_select(1, 'mat', 'Select SPM M/EEG file');
  25. if ~sts, D = []; return; end
  26. end
  27. P = spm_file(spm_file(P, 'ext', '.mat'), 'cpath');
  28. %-Load MAT file
  29. %--------------------------------------------------------------------------
  30. try
  31. load(P);
  32. catch
  33. try
  34. load(spm_file(P,'filename'))
  35. warning('Ignoring path. The dataset was loaded from the current directory.');
  36. catch
  37. error('Cannot load file "%s".', P);
  38. end
  39. end
  40. %-Check whether there is a struct D
  41. %--------------------------------------------------------------------------
  42. if ~exist('D','var')
  43. error('File "%s" doesn''t contain SPM M/EEG data.', P);
  44. end
  45. %-Handle situations where the object has been directly saved in file
  46. %--------------------------------------------------------------------------
  47. if ~isa(D, 'struct')
  48. try
  49. D = struct(D);
  50. catch
  51. error('The file should contain an SPM M/EEG struct named D.');
  52. end
  53. end
  54. %-Save path and fname in structure
  55. %--------------------------------------------------------------------------
  56. D.path = spm_file(P, 'path');
  57. D.fname = spm_file(P, 'filename');
  58. %-And return an MEEG object
  59. %--------------------------------------------------------------------------
  60. D = meeg(D);