Question #6376

Write a script that will load data from a file into a matrix. I am confused on how to load data files and call functions in matlab.

Expert's answer

Suppose you have a file "mydata.txt" with the following contents

1 2 3

4

5 6 7 8

9 10 11 12

So here are numbers separeted by spaces '

'

Then the command

M = dlmread('mydata.txt',' ')

will read all

the file into the matrix M and print

M =

1 2 3

4

5 6 7 8

9 10 11 12

The first

argument of the command 'dlmread' is the file name, and the second one is the

separator of numbers.

If you numbers are separeted by tabsm, or commas use

the commands

M = dlmread('mydata.txt','\t')

or

M =

dlmread('mydata.txt',',')

respectively.

1 2 3

4

5 6 7 8

9 10 11 12

So here are numbers separeted by spaces '

'

Then the command

M = dlmread('mydata.txt',' ')

will read all

the file into the matrix M and print

M =

1 2 3

4

5 6 7 8

9 10 11 12

The first

argument of the command 'dlmread' is the file name, and the second one is the

separator of numbers.

If you numbers are separeted by tabsm, or commas use

the commands

M = dlmread('mydata.txt','\t')

or

M =

dlmread('mydata.txt',',')

respectively.

## Comments

## Leave a comment