read matrix from .txt

hello, can anyone tell me what do i have to do to read this matrix saved on a.txt file?
1 2 3 4
5 6 7 8
9 10 11 12
thx :)

 Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 20 Ott 2013
M=dlmread('filename.txt')

12 Commenti

lucas
lucas il 20 Ott 2013
Modificato: lucas il 20 Ott 2013
hi azzi! thx for the help! but is comining back this:
*>> M=dlmread('matlab1.txt')
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==> ÿþ1*
Azzi Abdelmalek
Azzi Abdelmalek il 20 Ott 2013
Modificato: Image Analyst il 20 Ott 2013
From your post, your file contains a matrix of numbers, is it true? Maybe your file contains also characters.
lucas
lucas il 20 Ott 2013
yes! i made that matrix on excel and save it to .txt
only contain this:
1 2 3 4
5 6 7 8
9 10 11 12
lucas
lucas il 20 Ott 2013
the conversion of .xls to .txt maded by excel add other things? if i open it with the notepad i see the matrix of numbers that i wrote and nothing else
1 2 3 4
5 6 7 8
9 10 11 12
Azzi Abdelmalek
Azzi Abdelmalek il 20 Ott 2013
You can import your data from your Excell file with xlsread. If your txt file contains only numbers, the code should work.
lucas
lucas il 20 Ott 2013
but my homework asked me to read .txt haha :(
Image Analyst
Image Analyst il 20 Ott 2013
Alternatively, try importdata() or save from Excel as a CSV file instead.
Cedric
Cedric il 20 Ott 2013
Modificato: Cedric il 20 Ott 2013
Open your file with e.g. the notepad or MATLAB editor, and copy/paste the content here, using {}Code formatting.
Image Analyst
Image Analyst il 20 Ott 2013
Or better yet, just attach the whole file with the paperclip icon.
lucas
lucas il 20 Ott 2013
thx man :)
Cedric
Cedric il 20 Ott 2013
Modificato: Cedric il 20 Ott 2013
Ok, there are special characters in your file, so the issue is probably the way you saved it with Excel. Try exporting the Excel file as CSV (Comma Separated Values) even if you name the file with a .txt extension.
lucas
lucas il 20 Ott 2013
thx mate!! works fine now with that comma thing :)

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by