%% Import Delimited Numeric Data
% This example shows how to import numeric data delimited by any single character using the writematrix function. Create a sample file, read the entire file, and then read a subset of the file starting at the specified location.
% Create a tab-delimited file named num.txt that contains a 4-by-4 numeric array and display the contents of the file.

rng('default')
A = randi(99,[4,4]);
writematrix(A,'num.txt','Delimiter','\t')
type('num.txt')

% Read the entire file. The readmatrix function determines the delimiter automatically and returns a 4-by-4 double array.

M = readmatrix('num.txt')

% Read only the rectangular block of data beginning from the second row, third column, in the file. Create an import options object and specify the columns and rows to import using the SelectedVariableNames and DataLines properties. Then, import the selected portion of the data from the file.

opts = detectImportOptions('num.txt');
opts.SelectedVariableNames = {'Var3','Var4'};
opts.DataLines = [2 4];
readmatrix('num.txt',opts)

% Copyright 2015 The MathWorks, Inc.