How to read a 2 column plain text file into Matlab?
Show older comments
Below is a function I have to write - fread2col (a fuction to read a 2 column plain text file into matlab). When I run the below function, matlab command window only displays the first data column and not both columns of data which is what I want the function to resultantly do.
Below is my code written thus far, help on this issue would be much appreciated.
Written code below:
function [x,y,n] = fread2col(infile)
prompt = 'Enter the name of the datafile to be read' % user enters the textfile name to be read by fread2col
infile = input(prompt,'s');
data = load(infile);
x = data(:,1); % column 1 of the data text file is assigned the variable x
y = data(:,2); % column 2 is assigned the variable y
n = length(x); % the number of data points in each column is assigned the variable n
This is as far as I have gotten as the function is not working as I want it to and I don't understand why this is the case.
Answers (1)
Star Strider
on 8 Nov 2014
It would help if you attached your text file. It is difficult to see if you are reading it correctly without having it to experiment with.
My only suggestion is to change the load call to:
data = load(infile, '-ascii');
since it is a text file. See if that produces the results you want.
5 Comments
Natasha
on 8 Nov 2014
Star Strider
on 8 Nov 2014
Edited: Star Strider
on 8 Nov 2014
This works perfectly for me with R2014b:
infile = 'hawaii_coast.dat.txt';
data = load(infile, '-ascii');
x = data(:,1); % column 1 of the data text file is assigned the variable x
y = data(:,2); % column 2 is assigned the variable y
When I do:
SizeData = size(data)
I get:
SizeData =
11613 2
and:
figure(1)
plot(x,y)
grid on
produces a map of the Hawaii Archipelago:

AMMAR ELHAMDO
on 5 Jun 2020
Thank you, it is realy help me.
Sivakumar Kaliyappan
on 24 Apr 2021
It is working
Thanks
Star Strider
on 25 Apr 2021
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!