Matlab reading text files

Source: Internet
Author: User
Tags numeric numeric value

How to read a text file in MATLAB

Today needs to do a MATLAB read TXT file, collected on the internet, found a few good, summed up, convenient for everyone (including me) use:

The following function is to take the Filein in the line written in the Fileout program, if you want to implement a specific number of lines, as long as a slight modification.

function Dataout=dataread (filein,fileout,line)

Fidin=fopen (Filein, ' R ');

Fidout=fopen (Fileout, ' w ');

nline=0;

While ~feof (Fidin)% determines whether the end of the file

Tline=fgetl (Fidin); % read line from file

nline=nline+1;

If Nline==line

fprintf (fidout, '%s\n ', tline);

Dataout=tline;

End

End

Fclose (Fidin);

Fclose (fidout);

%%%%%%%%%%%%%%%%%%%%%%%%%%

Call format: Dataout=dataread (filein,fileout,line)

If your TXT file data is in the form of a matrix, and no other text, use the following program to read any column of any row of data

A=textread (' Ll.txt ');

T=a (1:43,4:10);

1:43 is 1 to 43 rows, 4:10 is 4 to 10 column of data, of course, can also read only one data, if your MATLAB does not have textread function, directly from the MathWorks site download on line.

Introduce different methods of reading data according to different kinds of txt documents

Transferred from: http://hi.baidu.com/youngbrave/blog/item/878db31fcd4f220f304e15bb.html

A, pure data file (no letters and Chinese, pure numbers)

For this TXT document, it's much easier to read from the Matalb.

For example, a test.txt file with the content "17.901-1.1111 33.045

17.891-1.1286 33.045

17.884-1.1345 33.045 "

You can enter load test.txt in the command window and generate a test data file that is the same as the data in Test.txt, and the other way is in File/import data....../next/finish You can also generate a data file called Test.

Second, Chinese and English and data such as Test1.txt

"Hello

Welcome here.

Vibration Forum

vib.hit.edu.cn

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555 "

How does such a file read into the data?

There are a variety of methods, which are two relatively simple and practical.

Method One:

File/import Data....../next/finish

>> whos

Name Size Bytes Class

Data 5x4 Double Array

TextData 4x1 Cell Array

Grand Total is elements using 460 bytes

>> data

data =

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555

>> TextData

TextData =

Hello

' Welcome here '

' Vibration Forum '

' Vib.hit.edu.cn '

Method Two:

[A1,a2,a3,a4]=textread (' Test1.txt ', '%s%s%s%s ', ' Headerlines ', 4)

Description:%s can be a different form, related to the type of data read, such as the%n,%f can also be used here.

The number of%s here corresponds to [a1,a2,a3,a4].

>> [A1,a2,a3,a4]=textread (' Test1.txt ', '%s%s%s%s ', ' Headerlines ', 4)

A1 =

' 1 '

' 2 '

' 3 '

' 4 '

' 5 '

A2 =

' 11 '

' 22 '

' 33 '

' 44 '

' 55 '

A3 =

' 111 '

' 222 '

' 333 '

' 444 '

' 555 '

a4 =

' 1111 '

' 2222 '

' 3333 '

' 4444 '

' 5555 '

It is read in as a string, so there is a ".

————————————————————————————————

Third, Chinese data English confusion such as Test.txt

How are you doing

1 11 111 1111

Welcome here.

2 22 222 2222

Vibration Forum

3 33 333 3333

vib.hit.edu.cn

4 44 444 4444

5 55 555 5555

Description: This content format file is not possible with the above method.

The following is a method written by Chinamaker:

Fidin=fopen (' test.txt '); % Open Test2.txt file

Fidout=fopen (' Mkmatlab.txt ', ' W '); % Create MKMATLAB.txt file

While ~feof (Fidin)% determines whether the end of the file

Tline=fgetl (Fidin); % read line from file

If Double (Tline (1)) >=48&&double (Tline (1)) <=57% determines if the first character is a numeric value

fprintf (fidout, '%s\n\n ', tline); % if it is a numeric line, write this line of data to the file MKMATLAB.txt

Continue% if the non-digit continues the next cycle

End

End

Fclose (fidout);

Mk=importdata (' MKMATLAB.txt '); % will generate the MKMATLAB.txt file import workspace, the variable named MK, actually it does not show up

>> MK

MK =

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555

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.