(1) Use fscanf to read text
Syntax:
A = fscanf (fileid, Format)
A = fscanf (fileid, format, sizea)
[A, Count] = fscanf (...)
Parameter introduction:
Instance:
% 1、open the file fidgefopen('read file .txt ', 'R'); % 2. Read the file a = fscanf (FID,' % D'); % The read result is in the form of N rows and 1 columns, n is the number of numbers in the text file % 3. close the file fclose (FID );Specifically, the content in the Read File .txt is:
1 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 96 7 8 9 0
(2) textscan reads formatted text
Syntax:
C = textscan (fileid, formatspec)
C = textscan (fileid, formatspec, n)
C = textscan (STR, formatspec)
C = textscan (STR, formatspec, n)
C = textscan (___, name, value)
[C, position] = textscan (___)
Formatspec:
Instance:
% Use textscanto read and format the token file fidgefopen('getfile .txt ', 'R'); A = textscan (FID,' % d % D'); fclose (FID ); % result % A {1} % is used to access a specific column % ans = % 1% 2% 3% 4% 6
(3) Ignore text headerlines
% Fid=fopen('read file .txt ', 'R'); A = textscan (FID,' % d % d', 'headerlines', 2 ); % ignore the two row headers fclose (FID );Result: The text is read from the third row.
MATLAB instance learning ----------- formatting text read Operations