www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ImportDelimitedNumericDataExample.m

    %% Import Delimited Numeric Data
% This example shows how to import numeric data delimited by any single character
% using the |dlmread| function. 
%%
% Create a tab-delimited file named |num.txt| that contains the following
% data:
%
% 
%  95    89    82    92
%  23    76    45    74
%  61    46    61    18
%  49     2    79    41
% 

% Copyright 2015 The MathWorks, Inc.



A = gallery('integerdata',99,[4,4],0);
dlmwrite('num.txt',A,'\t')
%%
% The sample file, |num.txt|, resides in your current folder.
%%
% Read the entire file. The file name is the only required input argument
% to the |dlmread| function. |dlmread| determines the delimiter from the
% formatting of the file.
M = dlmread('num.txt')
%%
% |M| is a 4-by-4 |double| array containing the data from the file.
%%
% Read only the rectangular block of data beginning from the second row, third
% column, in the file. When using |dlmread|, row and column indices are zero-based. When you specify a specific range to read, you must
% also specify the delimiter. Use |'\t'| to indicate a tab delimiter.
N = dlmread('num.txt','\t',1,2)
%%
% |dlmread| returns a 3-by-2 |double| array.
%%
% Read only the first two columns. You can use spreadsheet notation to
% indicate the range, in this case, |'A1..B4'|. 
P = dlmread('num.txt','\t','A1..B4')