How to read a .txt file?

13 views (last 30 days)
Amanda Queiroz
Amanda Queiroz on 28 Jan 2022
Commented: Amanda Queiroz on 28 Jan 2022
I have a .txt file with 1000000 rows and 14 columns. I want to read in Matlab but I'm not getting to do it. I'm using these commands:
fileID = fopen('data.txt','r');
formatSpec = '%.4f'
A = fscanf(fileID,formatSpec)
The file content an array like this (with 1000000 rows and 14 columns):
-34.7963 -7.1508 26.9324 26.8133 93.7783 41.8818 113.8538 65.2745 48.8024 30.8768 55.4924 14.6723 6.7068 22.6966
-34.7963 -7.1553 25.5952 27.5086 86.3124 40.4655 109.2303 64.8125 45.9477 29.5626 53.7228 15.9957 5.3562 18.0334
Can you help me? Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jan 2022
A = readmatrix('data.txt');
Unless you have a sufficiently old MATLAB.
With that file format you could also just
A = load('data.txt');
which might be faster.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!