Utility Method for Reading TXT files in matlab

xiaoxiao2021-03-06  116

Thanks to the Dynamics and Control Technology Forum! ! This post is particularly thanks to Chinamaker, Wolfer555, midnight meteor, small people.

% Small people small finishing% www.dytrol.com% 2004.7.9

Document content form 1 (assumed file named TEST1.TXT): Hello Welcome to Dynamics and Control Technology Forum www.dytrol.com1 11 111 11112 22 222 22223 33 333 33334 44 444 44445 55 555 5555 How to read What? There are a variety of methods, and there are two more simple and practical. Method 1: File / Import Data ... / Next / Finish >> Whos Name Size BYTES CLASS

Data 5x4 160 Double Array Textdata 4x1 300 Cell Array

Grand Total is 54 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 to '' Dynamics and Control Technology Forum '' Www.dytrol.com 'Method 2: [A1, A2, A3, A4] = TextRead (' Test1.txt ','% S% S) % s% s', 'HeaderLines', 4) Description:% S can be other forms, related to the data type read, such as% N,% f, etc. The number of% s here and [A1, A2, A3, A4] are corresponding. >> [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' is read in the form of a string, so there is ''.

Document content form 2 (assuming file name Test2.txt):

Hello 1 11 1111 1111 Welcome to 2 22 222 2222 Dynamics and Simulation Control Forum 3 33 333 3333www.dytrol.com 444 444 44445 55 555 5555 Description: This content format is not available in the above method.

The following is a method written by chinamaker: fidin = fopen ('test2.txt');% Open TEST2.TXT file fidout = fopen ('mkmatlab.txt', 'W');% creates mkmatlab.txt file while ~ Feof (FIDIN)% judges whether it is the end of the file tLINE = FGETL (FIDIN);% from the file reads IF Double (TLINE (1))> = 48 && Double (TLINE (1)) <= 57% Judging whether the first character is value FPRINTF (FIDOUT, '% S / N / N', TLINE);% If it is a digital line, write this row data into file mkmatlab.txt continue% If it is non-number to continue the next loop Endendfclose (Fidout); mk = importdata (' Mkmatlab.txt ');% will import the generated mkmatlab.txt file into the workspace, the variable is named MK, in fact it does not display >> MK

MK =

1 11 111 1111 2 22 222 2222 3 33 333 3333 4 44 444 4444 5 55 555 5555

[This post has been edited by the author at 2004-7-12 8:59:40]

----------------------------------------------

Something Gained, Something Lost

Something Lost, Something Gained

转载请注明原文地址:https://www.9cbs.com/read-125672.html

New Post(0)