Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
09.02.2015
Размер:
324.1 Кб
Скачать

If true, textscan treats

consecutive delimiters as a single

delimiter. Only valid if you

specify the 'Delimiter' option.

ReturnOnError Determines behavior when TEXTSCAN 1 (true)

fails to read or convert. If true,

TEXTSCAN terminates without error

and returns all fields read. If

false, TEXTSCAN terminates with an

error and does not return an output

cell array.

TreatAsEmpty String(s) in the data file to None

treat as an empty value. Can be a

single string or cell array of

strings. Only applies to numeric

fields.

Whitespace White-space characters ' \b\t'

Examples:

Example 1: Read each column of a text file.

Suppose the text file 'mydata.dat' contains the following:

Sally Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i

Joe Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i

Bill Level3 34.90 12 2e5 10 100 No 3.1+.1i

Read the file:

fid = fopen('mydata.dat');

C = textscan(fid, '%s%s%f32%d8%u%f%f%s%f');

fclose(fid);

TEXTSCAN returns a 1-by-9 cell array C with the following cells:

C{1} = {'Sally','Joe','Bill'} %class cell

C{2} = {'Level1'; 'Level2'; 'Level3'} %class cell

C{3} = [12.34;23.54;34.9] %class single

C{4} = [45;60;12] %class int8

C{5} = [4294967295; 4294967295; 200000] %class uint32

C{6} = [Inf;-Inf;10] %class double

C{7} = [NaN;0.001;100] %class double

C{8} = {'Yes','No','No'} %class cell

C{9} = [5.1+3.0i; 2.2-0.5i; 3.1+0.1i] %class double

The first two elements of C{5} are the maximum values for a 32-bit

unsigned integer, or intmax('uint32').

Example 2: Read a string, truncating each value to one decimal digit.

str = '0.41 8.24 3.57 6.24 9.27';

C = textscan(str, '%3.1f %*1d');

TEXTSCAN returns a 1-by-1 cell array C:

C{1} = [0.4; 8.2; 3.5; 6.2; 9.2]

Example 3: Resume a text scan of a string.

lyric = 'Blackbird singing in the dead of night';

[firstword, pos] = textscan(lyric,'%9c', 1); %first word

lastpart = textscan(lyric(pos+1:end), '%s'); %remaining text

For additional examples, type "doc textscan" at the command prompt.

See also fopen, fclose, load, importdata, uiimport, dlmread, xlsread, fscanf, fread.

Reference page in Help browser

doc textscan

Соседние файлы в папке Библиотеки Matlab