diff --git a/NPMK/NSx Utilities/separatePausedNSx.m b/NPMK/NSx Utilities/separatePausedNSx.m index 44dd5f0..4cbfba1 100644 --- a/NPMK/NSx Utilities/separatePausedNSx.m +++ b/NPMK/NSx Utilities/separatePausedNSx.m @@ -1,89 +1,8 @@ function separatePausedNSx(varargin) -% separatePausedNsx Saves paused data segments from a single NSx file -% as individual NSx files -% -% separatePausedNSx(FILENAME) where FILENAME has N paused segments -% will create N individual files with the same extension named -% FILENAME-1, FILENAME-2, ..., FILENAME-N. -% -% separatePausedFiles without any input arguments opens a UIgetfile to -% select the NSx file to separate -% -% Brett Dowden -% bdowden@blackrockmicro.com -% Nick Halper -% nhalper@blackrockmicro.com -% Blackrock Microsystems -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Version History -% -% 1.0.0.0: Initial release -% -% 1.1.0.0: -% - Fixed a broken function that dependent on a non-existant saveNEV -% script. -% -% 1.2.0.0: -% - Corrected NSx_out.MetaTags.Filename = -% [NSx.MetaTags.Filename(1:end) '-p' sprintf('%03d', i) -% NSx.MetaTags.FileExt] which was incorrectly removing the last 4 -% characters of the filename in an attempt to remove the extension. -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% separatePausedNsx Obsoleted. Use splitNSxPauses instead. -% Since openNSx checks input parameters for file validity and for no input -% argument case, just pass varargin to openNSx -NSx = openNSx('read',varargin{:}); +disp('This function is obsoleted.') +disp('Use splitNSxPauses instead.'); -% Check to see if there were pauses in the file. If so, save the sections -% as individual files, if not just report that to the user and end function -if length(NSx.MetaTags.Timestamp) == 1 - disp('No pauses in data file. No action taken'); -else - for i = 1 : length(NSx.MetaTags.Timestamp) - % create a copy of NSx, except with only one data segment and the - % corresponding Timestamp, DataPoints, and NumofPacket fields - NSx_out.MetaTags = NSx.MetaTags; - NSx_out.MetaTags.Timestamp = NSx.MetaTags.Timestamp(i); - NSx_out.MetaTags.DataPoints = NSx.MetaTags.DataPoints(i); - NSx_out.MetaTags.NumofPackets = NSx.MetaTags.DataPoints(i); - - NSx_out.ElectrodesInfo = NSx.ElectrodesInfo; - - NSx_out.Data = NSx.Data{i}; - NSx_out.RawData.Headers = NSx.RawData.Headers; - %Data headers are only 9 bytes, so only check for the correct, - %respective 9 bytes - NSx_out.RawData.DataHeader = NSx.RawData.DataHeader(1+(9*(i-1)):9+(9*(i-1))); - - % Create enumerated file name - NSx_out.MetaTags.Filename = [NSx.MetaTags.Filename(1:end) '-p' sprintf('%03d', i) NSx.MetaTags.FileExt]; - disp('Opening the original file...'); - - -% Check for filename existence -newFilename = fullfile(NSx.MetaTags.FilePath, NSx_out.MetaTags.Filename); -if exist(newFilename, 'file') == 2 - overwriteFlag = input('The file already exists. Overwrite? ', 's'); - if ~strcmpi(overwriteFlag, 'y') - return; - end -end - - -% Create new file -FIDw = fopen(newFilename, 'w+', 'ieee-le'); - - -% Writing the header information -fwrite(FIDw, NSx_out.RawData.Headers, 'uint8'); -fwrite(FIDw, NSx_out.RawData.DataHeader, 'uint8'); - - -% Writing data into file -disp('Writing into the new file...'); -fwrite(FIDw, NSx_out.Data, 'int16'); -fclose(FIDw); - end - clear all; -end \ No newline at end of file +splitNSxPauses; \ No newline at end of file diff --git a/NPMK/NSx Utilities/splitNSxPauses.m b/NPMK/NSx Utilities/splitNSxPauses.m new file mode 100644 index 0000000..e95d87c --- /dev/null +++ b/NPMK/NSx Utilities/splitNSxPauses.m @@ -0,0 +1,133 @@ +function splitNSxPauses(fileName) + +% splitNSxPauses +% +% Opens and splits an NSx file in smaller pieces, timewise. +% +% Use splitNSx(fileName) +% +% All input arguments are optional. Input arguments can be in any order. +% +% fileName: File name of the file that needs to be split. +% DEFAULT: The user will be prompted to select a file. +% +% Example 1: +% splitNSx('C:\Datafolder\mydata.ns5'); +% +% In the example above, the file C:\Datafolder\mydata.ns5 will be opened. +% The loaded file will be split in samller files representing its paused +% sub-segments. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Kian Torab +% support@blackrockmicro.com +% Blackrock Microsystems +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% Version History +% +% 1.0.0.0: August 31, 2016 +% - Initial release. +% - Successor to separateNSxPaused running much more memory efficient. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%% Getting the file name +if ~exist('fileName', 'var') + fileName = ''; +end + +if ~(exist(fileName, 'file') == 2) + if ~ismac + [fname, path] = getFile('*.ns*', 'Choose an NSx file...'); + else + [fname, path] = getFile('*.*', 'Choose an NSx file...'); + end + if fname == 0 + disp('No file was selected.'); + if nargout + clear variables; + end + return; + end + fext = fname(end-3:end); +else + [path, fname, fext] = fileparts(fileName); + if ismac path = [path '/']; else path = [path '\']; end + fname = [fname fext]; +end + +%% Getting header information +NSx = openNSx('noread', [path fname ]); + +% Loading the file +%% Reading Basic Header from file into NSx structure. +FID = fopen([path fname], 'r', 'ieee-le'); +NSx.MetaTags.Filename = fname; +NSx.MetaTags.FilePath = path(1:end-1); +NSx.MetaTags.FileExt = fext; +NSx.MetaTags.FileTypeID = fread(FID, [1,8] , '*char'); +if strcmpi(NSx.MetaTags.FileTypeID, 'NEURALSG') + disp('File type 2.1 is not yet implemented.'); + %NOT IMPLEMENTED YET +% fseek(FID, 0, 'bof'); +% header = fread(FID, 314,'*uint8'); +% positionEOH = ftell(FID); +% fseek(FID, 0, 'eof'); +% positionEOD = ftell(FID); +% dataLength = positionEOD - positionEOH; +% fseek(FID, 28, 'bof'); +% channelCount = fread(FID, 1 , 'uint32=>double'); +elseif strcmpi(NSx.MetaTags.FileTypeID, 'NEURALCD') + % Calculating different points in the file + fseek(FID, 0, 'bof'); + basicHeader = fread(FID, 314, '*uint8'); + positionEOE = typecast(basicHeader(11:14), 'uint32'); + fseek(FID, 0, 'eof'); + positionEOD = ftell(FID); + % Calculating channelCount, data Length + channelCount = typecast(basicHeader(311:314), 'uint32'); + dataLength = positionEOD - positionEOE - 9; + % Reading the number of packets + fseek(FID, 28, 'bof'); + numOfPackets = (dataLength)/(2*channelCount); + % Calculating the number of splits + splitCount = length(NSx.MetaTags.Timestamp); + % Calculating the number of bytes in each segment + segmentBytes = NSx.MetaTags.DataPoints * 2 * double(channelCount); + % Reading the headers and the data header + fseek(FID, 0, 'bof'); + fileHeader = fread(FID, positionEOE, 'char'); + dataHeader = fread(FID, 9, 'char'); + fseek(FID, positionEOE, 'bof'); + disp(['Splitting the NSx file in ' num2str(splitCount) ' pieces...']); + for idx = 1:splitCount + % Opening a file for saving + FIDw = fopen([path fname(1:end-4) '-s' sprintf('%03d', idx) fname(end-3:end)], 'w+', 'ieee-le'); + fprintf('\nReading segment %d... ', idx); + % Reading the segment + fseek(FID, 9, 'cof'); % Skipping the data header + dataSegment = fread(FID, segmentBytes(idx), 'char'); + fprintf('Writing segment %d... ', idx); + % Writing the segmented data into file + fwrite(FIDw, fileHeader, 'char'); + % Set the timestamp of the segments 2+ to 0 so there's no + % introduced shift by openNSx. + if idx > 1 + dataHeader(2:5) = 0; + end + fwrite(FIDw, dataHeader, 'char'); + fwrite(FIDw, dataSegment, 'char'); + % Clearing variables and closing file + clear dataSegment; + fclose(FIDw); + end + fprintf('\n'); +else + % Display error if non-compatible file is trying to open. + disp('This version of splitNSx can only split File Specs 2.2 and 2.3'); + disp(['The selected file spec is ' NSx.MetaTags.FileSpec '.']); + fclose(FID); + clear variables; + return; +end diff --git a/NPMK/Versions.txt b/NPMK/Versions.txt index 473a4fa..ea1ecd3 100644 --- a/NPMK/Versions.txt +++ b/NPMK/Versions.txt @@ -327,4 +327,12 @@ NPMK Version 2.8.2.0: 5 May 2014 % available. % openNSx 6.3.0.0: August 3, 2016 -% - Added support for loading a segment of paused files. \ No newline at end of file +% - Added support for loading a segment of paused files. + +***** NPMK Version 4.4.1.0 August 31, 2016 ***** + +% splitNSxPauses: 1.0.0.0: August 31, 2016 +% - Initial release. +% - Successor to separateNSxPaused running much more memory efficient. + +% separateNSxPaused: This function is obsoleted. Use splitNSxPauses instead.