%% Read File Contents into Array
% Create a sample text file that contains integers and floating-point numbers.

x = 1:1:5;
y = [x;rand(1,5)];
fileID = fopen('nums2.txt','w');
fprintf(fileID,'%d %4.4f\n',y);
fclose(fileID);

%
% View the contents of the file.

type nums2.txt

%
% Open the file for reading, and obtain the file identifier, fileID.

fileID = fopen('nums2.txt','r');

%
% Define the format of the data to read and the shape of the output array.

formatSpec = '%d %f';
sizeA = [2 Inf];

%
% Read the file data, filling output array, A, in column order. fscanf reuses the format, formatSpec, throughout the file.

A = fscanf(fileID,formatSpec,sizeA)
fclose(fileID);

%
% Transpose the array so that A matches the orientation of the data in the file.

A = A'

% Copyright 2015 The MathWorks, Inc.