combineNSxNEV.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. %
  2. % [NSx, NEV] = combineNSxNEV(filename1, filename2)
  3. %
  4. % This function loads two NSx and NEV files and it will combine them
  5. % together into a sinlge NSx and NEV structure in MATLAB. To merge two
  6. % NSx and NEV files into individual files see mergeNSxNEV. The time
  7. % difference between the two sets of recordings is removed. To determine
  8. % the time differnce between the two data files, use
  9. % NSx.MetaTags.DateTimeRaw or NEV.MetaTags.DateTimeRaw variables.
  10. %
  11. %
  12. % filename1: The name of the first NSx file. This input is optional. In
  13. % its absense, a dialog will open and will prompt the user to
  14. % select an NSx file.
  15. % (OPTIONAL)
  16. %
  17. % filename2: The name of the second NSx file. This input is also
  18. % optional. In its absense, a dialog will open and will
  19. % prompt the user to select an NSx file.
  20. % (OPTIONAL)
  21. %
  22. %
  23. % Example:
  24. %
  25. % [NSx, NEV] = combineNSxNEV('c:\data\saveddata1.ns5', 'c:\data\saveddata2.ns5');
  26. %
  27. % The above example reads the two files (full path needed)
  28. % c:\data\saveddata1.ns5 and c:\data\saveddata2.ns5 and their corresponding
  29. % NEV files (saveddata1.nev and saveddata2.nev) in the same folder and
  30. % combines them into single variables NSx and NEV into MATLAB workspace.
  31. %
  32. % Kian Torab
  33. % ktorab@blackrockmicro.com
  34. % Blackrock Microsystems
  35. %
  36. % Version 1.1.2.0
  37. function [NSx1, NEV1] = combineNSxNEV(filename1, filename2)
  38. % Openning NSx files
  39. if exist('filename1', 'var') && exist('filename2', 'var')
  40. disp('Load the first NSx file.');
  41. NSx1 = openNSx('read', filename1);
  42. disp('Load the second NSx file.');
  43. NSx2 = openNSx('read', filename2);
  44. if NSx1.MetaTags.SamplingFreq ~= NSx2.MetaTags.SamplingFreq;
  45. disp('The sampling frequencies are not the same.');
  46. return;
  47. end
  48. else
  49. NSx1 = openNSx('read');
  50. NSx2 = openNSx('read');
  51. end
  52. % Determining length of the first NSx file
  53. conversionFactor = 30000/NSx1.MetaTags.SamplingFreq;
  54. NSx1DataLength = NSx1.MetaTags.DataPoints * conversionFactor;
  55. % Combining NSx files
  56. NSx1.Data = [NSx1.Data, NSx2.Data];
  57. % Opening NEV files
  58. fileNameNEV1 = [NSx1.MetaTags.FilePath '/' NSx1.MetaTags.Filename(1:end-3), 'nev'];
  59. fileNameNEV2 = [NSx1.MetaTags.FilePath '/' NSx2.MetaTags.Filename(1:end-3), 'nev'];
  60. clear NSx2;
  61. if (exist(fileNameNEV1, 'file') == 2) && (exist(fileNameNEV2, 'file') ==2)
  62. disp('Openning corresponding NEV files...');
  63. NEV1 = openNEV('read', fileNameNEV1);
  64. NEV2 = openNEV('read', fileNameNEV2);
  65. else
  66. disp('Load the first NEV file.');
  67. NEV1 = openNEV('read');
  68. disp('Load the second NEV file.');
  69. NEV2 = openNEV('read');
  70. end
  71. % Adjusting the timestamp on the second NEV file
  72. NEV2.Data.Comments.TimeStamp = NEV2.Data.Comments.TimeStamp + NSx1DataLength;
  73. NEV2.Data.Comments.TimeStampSec = NEV2.Data.Comments.TimeStampSec + double(NSx1DataLength)/30;
  74. NEV2.Data.SerialDigitalIO.TimeStamp = NEV2.Data.SerialDigitalIO.TimeStamp + NSx1DataLength;
  75. NEV2.Data.SerialDigitalIO.TimeStampSec = NEV2.Data.SerialDigitalIO.TimeStampSec + double(NSx1DataLength)/30;
  76. NEV2.Data.Spikes.TimeStamp = NEV2.Data.Spikes.TimeStamp + NSx1DataLength;
  77. NEV2.Data.VideoSync.TimeStamp = NEV2.Data.VideoSync.TimeStamp + NSx1DataLength;
  78. if ~isempty(NEV2.Data.Tracking)
  79. trackingFieldNames = fieldnames(NEV2.Data.Tracking);
  80. for idx = 1:size(trackingFieldNames, 1)
  81. NEV2.Data.Tracking.(trackingFieldNames{idx}).TimeStamp = NEV2.Data.Tracking.(trackingFieldNames{idx}).TimeStamp + NSx1DataLength;
  82. end
  83. end
  84. NEV2.Data.PatientTrigger.TimeStamp = NEV2.Data.PatientTrigger.TimeStamp + NSx1DataLength;
  85. NEV2.Data.Reconfig.TimeStamp = NEV2.Data.Reconfig.TimeStamp + NSx1DataLength;
  86. % Combining the two NEV files
  87. NEV1.Data.Spikes.Electrode = [NEV1.Data.Spikes.Electrode, NEV2.Data.Spikes.Electrode];
  88. NEV1.Data.Spikes.TimeStamp = [NEV1.Data.Spikes.TimeStamp, NEV2.Data.Spikes.TimeStamp];
  89. NEV1.Data.Spikes.Unit = [NEV1.Data.Spikes.Unit, NEV2.Data.Spikes.Unit];
  90. NEV1.Data.Spikes.Waveform = [NEV1.Data.Spikes.Waveform, NEV2.Data.Spikes.Waveform];
  91. NEV1.Data.Comments.TimeStamp = [NEV1.Data.Comments.TimeStamp, NEV2.Data.Comments.TimeStamp];
  92. NEV1.Data.Comments.TimeStampSec = [NEV1.Data.Comments.TimeStampSec, NEV2.Data.Comments.TimeStampSec];
  93. NEV1.Data.Comments.CharSet = [NEV1.Data.Comments.CharSet, NEV2.Data.Comments.CharSet];
  94. NEV1.Data.Comments.Color = [NEV1.Data.Comments.Color, NEV2.Data.Comments.Color];
  95. NEV1.Data.Comments.Text = [NEV1.Data.Comments.Text; NEV2.Data.Comments.Text];
  96. if ~isempty(NEV2.Data.Tracking)
  97. for idx = 1:size(trackingFieldNames, 1)
  98. NEV1.Data.Tracking.(trackingFieldNames{idx}).TimeStamp = [NEV1.Data.Tracking.(trackingFieldNames{idx}).TimeStamp, NEV2.Data.Tracking.(trackingFieldNames{idx}).TimeStamp];
  99. NEV1.Data.Tracking.(trackingFieldNames{idx}).TimeStampSec = [NEV1.Data.Tracking.(trackingFieldNames{idx}).TimeStampSec, NEV2.Data.Tracking.(trackingFieldNames{idx}).TimeStampSec];
  100. NEV1.Data.Tracking.(trackingFieldNames{idx}).ParentID = [NEV1.Data.Tracking.(trackingFieldNames{idx}).ParentID, NEV2.Data.Tracking.(trackingFieldNames{idx}).ParentID];
  101. NEV1.Data.Tracking.(trackingFieldNames{idx}).NodeCount = [NEV1.Data.Tracking.(trackingFieldNames{idx}).NodeCount, NEV2.Data.Tracking.(trackingFieldNames{idx}).NodeCount];
  102. NEV1.Data.Tracking.(trackingFieldNames{idx}).MarkerCount = [NEV1.Data.Tracking.(trackingFieldNames{idx}).MarkerCount, NEV2.Data.Tracking.(trackingFieldNames{idx}).MarkerCount];
  103. NEV1.Data.Tracking.(trackingFieldNames{idx}).MarkerCoordinates = [NEV1.Data.Tracking.(trackingFieldNames{idx}).MarkerCoordinates NEV2.Data.Tracking.(trackingFieldNames{idx}).MarkerCoordinates];
  104. end
  105. end