MATLAB Answers

Read data from a .txt file with two columns

131 views (last 30 days)
Lillian Lau
Lillian Lau on 30 Jul 2018
Commented: Lillian Lau on 30 Jul 2018
Hello, I have a .txt file with two jagged columns (example shown as code). How do I read these two columns into two different arrays? I have tried the load() and xlsread(), but I usually end up with one .xlsx file with both columns squished into one .xlsx column. Would appreciate suggestions, and thank you in advance!
1.33 333.2
1.5678 7.211
1.9 1.2346


Sign in to comment.

Answers (1)

Paolo on 30 Jul 2018
Edited: Paolo on 30 Jul 2018
If you want a cell array you can use:
fileID = fopen('mytext.txt');
mydata = textscan(fileID,'%f%f','HeaderLines',1);
If you prefer a table you can use:
t = readtable('mytext.txt','ReadVariableNames',false)

  1 Comment

Lillian Lau
Lillian Lau on 30 Jul 2018
Would the table be stored as an Excel file?

Sign in to comment.

Sign in to answer this question.