Reading a .dat file without a for loop

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.

3 comentarios

Stephen23
Stephen23 el 17 de Dic. de 2018
@David Stolnis: please upload a sample file.
Jan
Jan el 17 de Dic. de 2018
Editada: Jan el 17 de Dic. de 2018
Omit the useless commands.
frewind(fileID);
pnt=0;
status = fseek(fileID, pnt, 'bof');
tic;
David Stolnis
David Stolnis el 17 de Dic. de 2018
Thank you for the advice I will clean that up. That's a very good point I should be able to calculate, I'll need to make sure that's accurate. Time sampled data can get funky sometimes.

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 17 de Dic. de 2018

0 votos

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

2 comentarios

Brilliant. The following worked like a charm.
data = [fread(fileID,[22 Inf], 'int16')]';
data_length = size(data,1);
Thanks for the help!
Jan
Jan el 17 de Dic. de 2018
@David: The square brackets are not useful here. Simpler:
data = fread(fileID,[22 Inf], 'int16').';
Note: The square brackets are the operator for a concatenation, but you do not concatenate anything. Then quote ' is the ctranspose command using the complex conjugate. For real values this is the same result, but using the correct .' (with dot) is transpose.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 17 de Dic. de 2018
Editada: Jan el 17 de Dic. de 2018
Is the number of rows stored in the header? If not, isn't it easy to determine it by a simple calculation based on the file size?
Why not replacing
data = zeros(data_length, 33); % empty array (btw.: No, it is NOT empty)
for i=1:data_length
data(i, 1:22) = [fread(fileID, 22, 'int16')]';
end
by:
dataFile = fread(fileID, [22, inf], 'int16');
data = zeros(size(dataFile, 2), 33);
data(:, 1:22) = dataFile.';

Categorías

Productos

Versión

R2018b

Preguntada:

el 17 de Dic. de 2018

Comentada:

Jan
el 17 de Dic. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by