Rabu, 14 Mei 2008

How to read text file in Matlab?

Some times we use a matrix that have a big size to testing program that we write. If we must to input it from keyboard...hmmmm....how long time spend for it? So..we can write it to a text file.
For example:
Make a file text(for example data1.txt), that contains above:
25 26 67 68 89 79 28
27 24 16 28 29 16 29
23 35 27 28 29 11 12
87 23 56 79 25 67 25
Each data separated by space.

And for read this file please to type this code in command window:
>> x=load('data1.txt')
x =
25 26 67 68 89 79 28

27 24 16 28 29 16 29
23 35 27 28 29 11 12
87 23 56 79 25 67 25

If our file contain a string don't use LOAD instruction, but we can use TEXTREAD instruction.
For example:
Make a text file('data2.txt') with this sentences:
This is an example text file with string data, that will be read with Matlab.

And for read this file please to type this code in command window:
>> n=textread('data2.txt','%s')
n =
'This'

'is'
'an'
'example'
'text'
'file'
'with'
'string'
'data,'
'that'
'will'
'be'
'read'
'with'
'Matlab.'