From: Wayne King on
"Tyler " <cool_dude07(a)hotmail.com> wrote in message <huo3lp$feg$1(a)fred.mathworks.com>...
> Hey all,
> I am a new matlab user and want to know how to read in a matrix from a text file.
> Here is the file I want to read in called 'Sample.txt'
>
> 4
> 1 2 3 4
> 4 3 2 1
> 1 2 3 4
> 4 3 2 1
>
> I need to be able to read the first character (size of matrix) and then input into matlab and call matrix A=[1 2 3 4;4 3 2 1;1 2 3 4;4 3 2 1];
>
> Any help is appreciated,
>
> Thanks
> Tyler

Hi Tyler, One of many ways. if you just use textread() on a text file (let's call it example.txt)

Data = textread('example.txt');

If the contents of the text file are:

4
1 2 3 4
4 3 2 1
1 2 3 4
4 3 2 1


Then Data should contain

4 0 0 0
1 2 3 4
4 3 2 1
1 2 3 4
4 3 2 1

So you can then enter:
Data = Data(2:end,:);

Hope that helps,
Wayne