%% Read Digits of Binary Coded Decimal Values
% Create a file with binary coded decimal (BCD) values.

str = ['AB'; 'CD'; 'EF'; 'FA'];

fileID = fopen('bcd.bin','w');
fwrite(fileID,hex2dec(str),'ubit8');
fclose(fileID);

%
% Read 1 byte at a time.

fileID = fopen('bcd.bin');
onebyte = fread(fileID,4,'*ubit8');

%
% Display the BCD values.

disp(dec2hex(onebyte))

%
% Return to the beginning of the file using frewind. If you read 4 bits at a time on a little-endian system, your results appear in the wrong order.

frewind(fileID)

err = fread(fileID,8,'*ubit4');
disp(dec2hex(err))

%
% Return to the beginning of the file using frewind. Read the data 4 bits at a time as before, but specify a big-endian ordering to display the correct results.

frewind(fileID)

correct = fread(fileID,8,'*ubit4','ieee-be');
disp(dec2hex(correct))

%
% Close the file.

fclose(fileID);

% Copyright 2015 The MathWorks, Inc.