MATLAB instance learning ----------- formatting text read Operations

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.