MATLAB: Reading a .dat file without a for loop

for loopfreadMATLABtime series

for context… I'm trying to read a .dat file which contains 250 hz sample data. The following grabs the file and defines the header info:
%open .dat file and place cursor at beginning
[datName,datPath,~] = uigetfile('*.dat','Select .dat acceleration file'); % Prompt user to select acceleration file
fileID = fopen([datPath,datName]);
frewind(fileID);
pnt=0;
status = fseek(fileID, pnt, 'bof');
tic;
%convert data2 bytes to integers
header_1_2 = fread(fileID,1,'int16');
header_3_4 = fread(fileID,1,'int16');
header_5_36 = fread(fileID,16,'int16');
header_37_68 = fread(fileID, 16, 'int16');
header_69_100 = fread(fileID, 16, 'int16');
header_101_128 = fread(fileID, 14, 'int16');
The next piece of code is the part I need some assistance on. I currently create an empty matrix to run a for loop on the file with. Each row should have 22 channels/columns
% creates nx33 matrix of zeros where n (data_length) is # of points to end of file
data_length = 2000000; % 1M-2M rows usually allows enough to find portion of data needed
data = zeros(data_length, 33); % empty array
for i=1:data_length
data(i, 1:22) = [fread(fileID, 22, 'int16')]';
end
I dont want to be tied to 2 million row loop, I may need more or less. But I'm not sure how I can keep the 22 channels. To be a more robust code I want the number of row to match my acutal sample count as this is going to be used for auomation. Any advice on how to fread the entire file would be much appreciated! Thanks for the look.

Best Answer

I've answered a very similar question from somebody else earlier today. Use either the second or 3rd option I suggested.
Related Question