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 件のコメント
Stephen23
2018 年 12 月 17 日
@David Stolnis: please upload a sample file.
Omit the useless commands.
frewind(fileID);
pnt=0;
status = fseek(fileID, pnt, 'bof');
tic;
David Stolnis
2018 年 12 月 17 日
採用された回答
その他の回答 (1 件)
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.';
カテゴリ
ヘルプ センター および File Exchange で Low-Level File I/O についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!