%% Read Selected Rows or Columns from File
% Create a file named nine.bin, containing the values from 1 to 9. Write the data as uint16 values.

fileID = fopen('nine.bin','w');
fwrite(fileID,[1:9],'uint16');
fclose(fileID);

%
% Read the first six values into a 3-by-2 array. Specify that the source data is class uint16.

fileID = fopen('nine.bin');
A = fread(fileID,[3,2],'uint16')

% fread returns an array populated column-wise with the first six values from the file, nine.bin.
%
% Return to the beginning of the file.

frewind(fileID)

%
% Read two values at a time, and skip one value before reading the next values. Specify this format using the precision value, '2*uint16'. Because the data is class uint16, one value is represented by 2 bytes. Therefore, specify the skip argument as 2.

precision = '2*uint16';
skip = 2;
B = fread(fileID,[2,3],precision,skip)

% fread returns a 2-by-3 array populated column-wise with the values from nine.bin.
%
% Close the file.

fclose(fileID);

% Copyright 2015 The MathWorks, Inc.