-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrn_vread.m
51 lines (44 loc) · 1.15 KB
/
nrn_vread.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
% [data,errmsg]=nrn_vread(FileName,machineformat)
% machineformat like in fopen (i.e. 'b' for big endian, 'l' for little endian)
%
% reads binary files, that was written with Vector.vwrite in NEURON
%
% Written by user miller on NEURON forums
% accessed 5/15/2015
% http://www.neuron.yale.edu/phpbb/viewtopic.php?f=8&t=134
function [data,errmsg]=nrn_vread(FileName,machineformat)
data = [];
[fid,errmsg] = fopen(FileName,'r',machineformat);
if fid==-1
return;
else
errmsg = sprintf('File opened successfully');
end
[header,cnt]=fread(fid,2,'int32');
if cnt~=2
errmsg = sprintf('Could not read the vwrite header');
fclose(fid);
return;
end
precision = 'double'; % to avoid Matlab warning
if header(2)==4
precision = 'double';
elseif header(2)==3
precision = 'float32';
elseif header(2)==5
precision = 'int';
else
errmsg = sprintf('Unsupported precision argument');
fclose(fid);
return;
end
[data,cnt]=fread(fid,inf,precision);
if cnt~=header(1)
errmsg = sprintf('Only %d instead of %d Samples read',cnt,header(1));
data = [];
fclose(fid);
return;
else
errmsg = sprintf('Successfully read %d Samples',cnt);
end
fclose(fid);