-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLX2MAT_convert.m
98 lines (78 loc) · 2.51 KB
/
NLX2MAT_convert.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
%% NLX to MAT conversion script
% converts one NLX recording to .MAT format
%
% t_stamps timestamps of each data packet (us)
% chans unsure
% fs sampling frequency of each data point (all same value)
% n_valid_samples number of valid samples (out of 512?)
% data 512 x samples of data (what does the 512 do)
% header recording information
function NLX2MAT_convert
addpath('MatlabImportExport_v6.0.0')
addpath('natsortfiles')
tic
% options
save_on = 1;
plot_on = 0; % will pause after every file
% get data directory - convert everything in this folder
datapath = uigetdir(pwd,'select NLX data folder');
outpath = uigetdir(pwd, 'select an output directory for MAT file');
chans = dir(fullfile(datapath, '*.ncs'));
chans = natsortfiles(extractfield(chans,'name'));
num_files = length(chans);
% setup data
NLX_data = {};
chans_fs = [];
t_start = [];
% for all files in directory
for idx = 1:num_files
% get names
ch = chans{idx};
ch = split(ch,'.');
ch = ch{1};
chans{idx} = ch;
filename = strcat(datapath,'\',chans{idx},'.ncs');
% convert data
[t_stamps, ~, fs, ~, data, header] = Nlx2MatCSC(filename,[1 1 1 1 1], 1, 1, [] );
% if first time in loop, setup proper matrix sizes
if idx == 1
NLX_data = cell(1,num_files);
chans_fs = zeros(1,num_files);
t_start = zeros(1,num_files);
end
% convert data from ADC to voltage
v_convert = split(header{17,1},' ');
v_convert = str2double(v_convert{2});
ECOG = data.* v_convert;
NLX_data{idx} = reshape(ECOG,[1 size(data,1)*size(data,2)]);
% modify data so it's useable
chans_fs(idx) = fs(1);
t_stamps = t_stamps / 1e6; % convert to seconds
t_start(idx) = t_stamps(1);
% t = 1/fs:1/fs:length(ECOG)/fs;
% plot data
if plot_on == 1
figure
plot(t,ECOG.*1e3)
xlabel('time (s)')
ylabel('voltage (mV)')
title(sprintf('%s',ch))
pause
end
% save data to struct
% NLX_data(idx).fs = fs;
% NLX_data(idx).header = header;
% NLX_data(idx).t = t;
% NLX_data(idx).t_start = t_start;
% NLX_data(idx).ECOG = ECOG;
fprintf('Successfully converted %s\n',ch)
end
% save data to file
disp('Saving to file...')
if save_on == 1
out_name = split(datapath,'\');
outpath_name = strcat(outpath,'\',out_name{end},'.mat');
save(outpath_name,'NLX_data','chans','chans_fs','t_start','-v7.3')
fprintf('Finished in %.2f seconds\n',toc)
end
end