-
Notifications
You must be signed in to change notification settings - Fork 1
/
writeNexFile.m
406 lines (360 loc) · 14 KB
/
writeNexFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
function [result] = writeNexFile(nexFile, fileName)
% Modified Adam Rouse, 5/4/16
% modified writing of waveforms to be exactly like nex file exported from
% plexon
% [result] = writeNexFile(nexFile, fileName) -- write nexFile structure
% to the specified .nex file. returns 1 if succeeded, 0 if failed.
%
% INPUT:
% nexFile - a structure containing .nex file data
%
% SOME FIELDS OF THIS STRUCTURE (VERSIONS ETC.) ARE NOT DESCRIBED
% BELOW. IT IS RECOMMENDED THAT YOU READ A VALID .NEX FILE
% TO FILL THIS STRUCTURE, THEN MODIFY THE STRUCTURE AND SAVE IT.
%
% IF YOU WANT TO CREATE NEW .NEX FILE, USE nexCreateFileData.m,
% nexAddContinuous.m etc. See exampleSaveDataInNexFile.m.
%
% fileName - if empty string, will use File Save dialog
%
% nexFile - a structure containing .nex file data
% nexFile.version - file version
% nexFile.comment - file comment
% nexFile.tbeg - beginning of recording session (in seconds)
% nexFile.tend - end of recording session (in seconds)
%
% nexFile.neurons - array of neuron structures
% neurons{i}.name - name of a neuron variable
% neurons{i}.timestamps - array of neuron timestamps (in seconds)
% to access timestamps for neuron 2 use {n} notation:
% nexFile.neurons{2}.timestamps
%
% nexFile.events - array of event structures
% events{i}.name - name of event variable
% events{i}.timestamps - array of event timestamps (in seconds)
%
% nexFile.intervals - array of interval structures
% intervals{i}.name - name of interval variable
% intervals{i}.intStarts - array of interval starts (in seconds)
% intervals{i}.intEnds - array of interval ends (in seconds)
%
% nexFile.waves - array of wave structures
% waves{i}.name - name of waveform variable
% waves{i}.NPointsWave - number of data points in each wave
% waves{i}.WFrequency - A/D frequency for wave data points
% waves{i}.timestamps - array of wave timestamps (in seconds)
% waves{i}.waveforms - matrix of waveforms (in milliVolts), each
% waveform is a column
%
% nexFile.contvars - array of continuous variable structures
% contvars{i}.name - name of continuous variable
% contvars{i}.ADFrequency - A/D frequency for data points
%
% Continuous (a/d) data for one channel is allowed to have gaps
% in the recording (for example, if recording was paused, etc.).
% Therefore, continuous data is stored in fragments.
% Each fragment has a timestamp and an index of the first data
% point of the fragment (data values for all fragments are stored
% in one array and the index indicates the start of the fragment
% data in this array).
% The timestamp corresponds to the time of recording of
% the first a/d value in this fragment.
%
% contvars{i}.timestamps - array of timestamps (fragments start times in seconds)
% contvars{i}.fragmentStarts - array of start indexes for fragments in contvar.data array
% contvars{i}.data - array of data points (in milliVolts)
%
% nexFile.popvectors - array of population vector structures
% popvectors{i}.name - name of population vector variable
% popvectors{i}.weights - array of population vector weights
%
% nexFile.markers - array of marker structures
% markers{i}.name - name of marker variable
% markers{i}.timestamps - array of marker timestamps (in seconds)
% markers{i}.values - array of marker value structures
% markers{i}.value.name - name of marker value
% markers{i}.value.strings - array of marker value strings
%
result = 0;
if (nargin < 2 || isempty(fileName))
[fname, pathname] = uiputfile('*.nex', 'Save file name');
if isequal(fname,0)
error 'File name was not selected'
return
end
fileName = fullfile(pathname, fname);
end
% note 'l' option when opening the file.
% this options means that the file is 'little-endian'.
% this should ensure that the files are written correctly
% on big-endian systems, such as Mac G5.
fid = fopen(fileName, 'w', 'l', 'US-ASCII');
if(fid == -1)
error 'Unable to open file'
return
end
% count all the variables
neuronCount = 0;
eventCount = 0;
intervalCount = 0;
waveCount = 0;
contCount = 0;
markerCount = 0;
if(isfield(nexFile, 'neurons'))
neuronCount = size(nexFile.neurons, 1);
end
if(isfield(nexFile, 'events'))
eventCount = size(nexFile.events, 1);
end
if(isfield(nexFile, 'intervals'))
intervalCount = size(nexFile.intervals, 1);
end
if(isfield(nexFile, 'waves'))
waveCount = size(nexFile.waves, 1);
end
if(isfield(nexFile, 'contvars'))
contCount = size(nexFile.contvars, 1);
end
if(isfield(nexFile, 'markers'))
markerCount = size(nexFile.markers, 1);
end
nvar = int32(neuronCount+eventCount+intervalCount+waveCount+contCount+markerCount);
% write header information
fwrite(fid, 827868494, 'int32');
if(isfield(nexFile, 'version'))
fwrite(fid, nexFile.version, 'int32');
else
fwrite(fid, 106, 'int32');
end
writeStringPaddedWithZeros(fid, nexFile.comment, 256);
fwrite(fid, nexFile.freq, 'double');
fwrite(fid, int32(nexFile.tbeg*nexFile.freq), 'int32');
if(isfield(nexFile, 'tend'))
fwrite(fid, int32(nexFile.tend*nexFile.freq), 'int32');
else
% write fake end equal to beg
fwrite(fid, int32(nexFile.tbeg*nexFile.freq), 'int32');
end
fwrite(fid, nvar, 'int32');
% skip location of next header and padding
fwrite(fid, char(zeros(1, 260)), 'char');
% calculate where variable data starts
dataOffset = 544 + nvar*208;
% write variable headers
varHeader.Type = 0;
varHeader.Version = 100;
varHeader.Name = ' ';
varHeader.DataOffset = 0;
varHeader.Count = 0;
varHeader.WireNumber = 0;
varHeader.UnitNumber = 0;
varHeader.Gain = 0;
varHeader.Filter = 0;
varHeader.XPos = 0;
varHeader.YPos = 0;
varHeader.WFrequency = 0;
varHeader.ADtoMV = 0;
varHeader.NPointsWave = 0;
varHeader.NMarkers = 0;
varHeader.MarkerLength = 0;
varHeader.MVOffset = 0;
varHeader.PrethresholdTimeInSeconds = 0;
% write neuron headers
for i = 1:neuronCount
varHeader.Type = 0;
varHeader.Version = 100;
varHeader.Name = nexFile.neurons{i}.name;
varHeader.Count = size(nexFile.neurons{i}.timestamps,1);
varHeader.DataOffset = dataOffset;
varHeader.WireNumber = 0;
varHeader.UnitNumber = 0;
varHeader.XPos = 0;
varHeader.YPos = 0;
if(isfield(nexFile.neurons{i}, 'varVersion'))
varHeader.Version = nexFile.neurons{i}.varVersion;
end
if(isfield(nexFile.neurons{i}, 'wireNumber'))
varHeader.WireNumber = nexFile.neurons{i}.wireNumber;
end
if(isfield(nexFile.neurons{i}, 'unitNumber'))
varHeader.UnitNumber = nexFile.neurons{i}.unitNumber;
end
if(isfield(nexFile.neurons{i}, 'xPos'))
varHeader.XPos = nexFile.neurons{i}.xPos;
end
if(isfield(nexFile.neurons{i}, 'yPos'))
varHeader.YPos = nexFile.neurons{i}.yPos;
end
writeNexVarHeader(fid, varHeader);
dataOffset = dataOffset + varHeader.Count*4;
end
% event headers
varHeader.Version = 100;
varHeader.WireNumber = 0;
varHeader.UnitNumber = 0;
varHeader.XPos = 0;
varHeader.YPos = 0;
for i = 1:eventCount
varHeader.Type = 1;
varHeader.Name = nexFile.events{i}.name;
varHeader.Count = size(nexFile.events{i}.timestamps,1);
varHeader.DataOffset = dataOffset;
writeNexVarHeader(fid, varHeader);
dataOffset = dataOffset + varHeader.Count*4;
end
% interval headers
for i = 1:intervalCount
% interval variable type is 2
varHeader.Type = 2;
varHeader.Name = nexFile.intervals{i}.name;
varHeader.Count = size(nexFile.intervals{i}.intStarts,1);
varHeader.DataOffset = dataOffset;
writeNexVarHeader(fid, varHeader);
dataOffset = dataOffset + varHeader.Count*8;
end
% wave headers
for i = 1:waveCount
% we need to recalculate a/d to millivolts factor
if ~isfield(nexFile.waves{i}, 'ADtoMV') %AGR, 5/4/2016
wmin = min(min(nexFile.waves{i}.waveforms));
wmax = max(max(nexFile.waves{i}.waveforms));
c = max(abs(wmin),abs(wmax));
if (c == 0)
c = 1;
else
c = c/32767;
end
nexFile.waves{i}.ADtoMV = c;
end
nexFile.waves{i}.MVOffset = 0;
% wave variable type is 3
varHeader.Type = 3;
varHeader.Version = 100;
varHeader.Name = nexFile.waves{i}.name;
varHeader.Count = size(nexFile.waves{i}.timestamps,1);
varHeader.DataOffset = dataOffset;
varHeader.WireNumber = 0;
varHeader.UnitNumber = 0;
varHeader.WFrequency = nexFile.waves{i}.WFrequency;
varHeader.ADtoMV = nexFile.waves{i}.ADtoMV;
varHeader.NPointsWave = nexFile.waves{i}.NPointsWave;
varHeader.MVOffset = nexFile.waves{i}.MVOffset;
if(isfield(nexFile.waves{i}, 'varVersion'))
varHeader.Version = nexFile.waves{i}.varVersion;
end
if(isfield(nexFile.waves{i}, 'wireNumber'))
varHeader.WireNumber = nexFile.waves{i}.wireNumber;
end
if(isfield(nexFile.waves{i}, 'unitNumber'))
varHeader.UnitNumber = nexFile.waves{i}.unitNumber;
end
if(isfield(nexFile.waves{i}, 'PrethresholdTimeInSeconds'))
varHeader.PrethresholdTimeInSeconds = nexFile.waves{i}.PrethresholdTimeInSeconds;
end
writeNexVarHeader(fid, varHeader);
dataOffset = dataOffset + varHeader.Count*4 + varHeader.NPointsWave*varHeader.Count*2;
end
% continuous variables
varHeader.Version = 100;
varHeader.WireNumber = 0;
varHeader.UnitNumber = 0;
varHeader.PrethresholdTimeInSeconds = 0;
for i = 1:contCount
wmin = min(min(nexFile.contvars{i}.data));
wmax = max(max(nexFile.contvars{i}.data));
c = max(abs(wmin),abs(wmax));
if (c == 0)
c = 1;
else
c = c/32767;
end
nexFile.contvars{i}.ADtoMV = c;
nexFile.contvars{i}.MVOffset = 0;
% cont. variable type is 5
varHeader.Type = 5;
varHeader.Version = 100;
varHeader.Name = nexFile.contvars{i}.name;
varHeader.Count = size(nexFile.contvars{i}.timestamps,1);
varHeader.DataOffset = dataOffset;
varHeader.WFrequency = nexFile.contvars{i}.ADFrequency;
varHeader.ADtoMV = nexFile.contvars{i}.ADtoMV;
varHeader.NPointsWave = size(nexFile.contvars{i}.data, 1);
varHeader.MVOffset = nexFile.contvars{i}.MVOffset;
writeNexVarHeader(fid, varHeader);
% we have timestamps and indexes, so we use 8 bytes per count
dataOffset = dataOffset + varHeader.Count*8 + varHeader.NPointsWave*2;
end
% markers
varHeader.WFrequency = 0;
varHeader.ADtoMV = 0;
varHeader.NPointsWave = 0;
varHeader.NMarkers = 0;
varHeader.MarkerLength = 0;
varHeader.MVOffset = 0;
for i = 1:markerCount
nexFile.markers{i}.NMarkers = size(nexFile.markers{i}.values, 1);
nexFile.markers{i}.MarkerLength = 0;
if (nexFile.markers{i}.NMarkers > 0)
% check the first marker field
if(isfield(nexFile.markers{i}.values{1,1}, 'numericValues'))
% convert to strings
disp('markers are stored as numbers, converting marker values to strings');
for field = 1:nexFile.markers{i}.NMarkers
for item = 1:size(nexFile.markers{i}.values{field,1}.numericValues, 1)
nexFile.markers{i}.values{field,1}.strings{item,1} = sprintf('%d', nexFile.markers{i}.values{field,1}.numericValues(item));
end
end
end
end
MarkerLength = 0;
for j = 1:nexFile.markers{i}.NMarkers
for k = 1:size(nexFile.markers{i}.values{j,1}.strings, 1)
MarkerLength = max(MarkerLength, size(nexFile.markers{i}.values{j,1}.strings{k,1}, 2));
end
end
% add extra char to hold zero (end of string)
MarkerLength = MarkerLength + 1;
nexFile.markers{i}.MarkerLength = MarkerLength;
% marker variable type is 6
varHeader.Type = 6;
varHeader.Version = 100;
varHeader.Name = nexFile.markers{i}.name;
varHeader.Count = size(nexFile.markers{i}.timestamps,1);
varHeader.DataOffset = dataOffset;
varHeader.NMarkers = nexFile.markers{i}.NMarkers;
varHeader.MarkerLength = nexFile.markers{i}.MarkerLength;
writeNexVarHeader(fid, varHeader);
dataOffset = dataOffset + varHeader.Count*4 + nexFile.markers{i}.NMarkers*64 + nexFile.markers{i}.NMarkers*varHeader.Count*MarkerLength;
end
for i = 1:neuronCount
fwrite(fid, nexFile.neurons{i}.timestamps.*nexFile.freq, 'int32');
end
for i = 1:eventCount
fwrite(fid, nexFile.events{i}.timestamps.*nexFile.freq, 'int32');
end
for i = 1:intervalCount
fwrite(fid, nexFile.intervals{i}.intStarts.*nexFile.freq, 'int32');
fwrite(fid, nexFile.intervals{i}.intEnds.*nexFile.freq, 'int32');
end
for i = 1:waveCount
fwrite(fid, nexFile.waves{i}.timestamps.*nexFile.freq, 'int32');
% wf = int16(nexFile.waves{i}.waveforms./nexFile.waves{i}.ADtoMV);
wf = int16(nexFile.waves{i}.waveforms./nexFile.waves{i}.ADtoMV);
fwrite(fid, wf, 'int16');
end
for i = 1:contCount
fwrite(fid, nexFile.contvars{i}.timestamps.*nexFile.freq, 'int32');
fwrite(fid, nexFile.contvars{i}.fragmentStarts - 1, 'int32');
fwrite(fid, int16(nexFile.contvars{i}.data./nexFile.contvars{i}.ADtoMV), 'int16');
end
for i = 1:markerCount
fwrite(fid, nexFile.markers{i}.timestamps.*nexFile.freq, 'int32');
for j = 1:nexFile.markers{i}.NMarkers
writeStringPaddedWithZeros(fid, nexFile.markers{i}.values{j,1}.name, 64);
for k = 1:size(nexFile.markers{i}.values{j,1}.strings, 1)
writeStringPaddedWithZeros( fid, nexFile.markers{i}.values{j,1}.strings{k,1}, nexFile.markers{i}.MarkerLength );
end
end
end
fclose(fid);
result = 1;