-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWidefield_LoadData.m
52 lines (48 loc) · 3.18 KB
/
Widefield_LoadData.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
function [header,data] = Widefield_LoadData(cPath,Condition,dataType,pInd)
% short routine to load data from WidefieldImager code.
% cPath is the path of the file that should be opened. Condition is the
% type of data file which will determine the way the data file is read.
% Optional input 'pInd' defines a single pixel from which a data vector should
% be extracted. pInd is a two-value vector for x-y pixel coordinates. This
% means X and Y for image coordinates NOT matlab coordinates (which are
% usually inverse).
if ~exist('dataType','var') || isempty(dataType)
dataType = 'uint16';
end
if ~exist('pInd','var') || isnan(pInd)
pInd = [];
else
if length(pInd) ~= 2 || ~isnumeric(pInd)
error('Invalid input for index of selected pixel')
end
end
fID = fopen(cPath);
switch lower(Condition)
case 'analog'
hSize = fread(fID,1,'double'); %header size
header = fread(fID,hSize,'double'); %Metadata. Default is: 1 = Time of Acquisition onset, 2 = Number of channels, 3 = number of values per channel
data = fread(fID,[header(end-1),header(end)],[dataType '=>' dataType]); %get data. Last 2 header values should contain the size of the data array.
case 'frames'
hSize = fread(fID,1,'double'); %header size
header = fread(fID,hSize,'double'); %Metadata. Default is: 1:x = Absolute timestamps for each frame, Last 4 values: Size of each dimensions in the matrix
if ~isempty(pInd) %if extracting single pixel information
imSize = (header(find(diff(header) < -1e3) + 1)*header(find(diff(header) < -1e3) + 2))-1; %number of datapoints to make a single image minus one. skip that many datapoints to stick to the same pixel when using fread.
imStart = ((pInd(1)-1)*header(find(diff(header) < -1e3) + 1))+pInd(2)-1; %first value for selected pixel
fseek(fID,imStart*2,'cof'); %shift file pointer to the right pixel to start data extraction from file
data = fread(fID,header(find(diff(header) < -1e3) + 4),[dataType '=>' dataType],imSize*2); %get data.
if length(data) ~= header(end)
error('Could not extract all data values from pixel')
end
else
data = fread(fID,[prod(header(find(diff(header) < -1e3) + 1 : end)),1],[dataType '=>' dataType]); %get data. Last 4 header values should contain the size of the data array.
if length(data) ~= prod(header(find(diff(header) < -1e3) + 1 : end)) %if insufficient data is found in .dat file. Sometimes fread does not get all values from file when reading from server.
fclose(fID);fID = fopen(cPath); %try loading data again
hSize = fread(fID,1,'double'); %header size
header = fread(fID,hSize,'double'); %Metadata. Defautlt is: 1:x = Absolute timestamps for each frame, Last 4 values: Size of each dimensions in the matrix
data = fread(fID,[prod(header(find(diff(header) < -1e3) + 1 : end)),1],[dataType '=>' dataType]); %get data. Last 4 header values should contain the size of the data array.
end
data = reshape(data,header(find(diff(header) < -1e3) + 1 : end)'); %reshape data into matrix
end
end
fclose(fID);
end