reading a file in matlab
Show older comments
hello,
I have a file like below named file.txt
> head(myfile,1:4])
AT1G01060 AT1G01170 AT1G01260 AT1G01380
AT1G01060 1.00000000 0.3885284 -0.14720327 -0.01865947
AT1G01170 0.38852841 1.0000000 -0.29069241 0.26992353
AT1G01260 -0.14720327 -0.2906924 1.00000000 0.30973373
AT1G01380 -0.01865947 0.2699235 0.30973373 1.00000000
AT1G01490 0.24681279 0.3955740 -0.07497821 0.23271890
AT1G01500 0.05720335 -0.1786700 -0.26813919 -0.60440141
> dim(myfile)
[1] 2885 2885
please someone help me to read this file in matlab I was really exhausted
thank you
9 Comments
Ken Atwell
on 12 Feb 2016
Can you clean up the formatting? I can't tell where the file content begin, or where line breaks may be.
fereshteh izadi
on 12 Feb 2016
Image Analyst
on 12 Feb 2016
Edited: Image Analyst
on 12 Feb 2016
You forgot to attach file.txt. Make it easy for us to help you, not hard.
How did that file get created? Can you put a column header over the first row? If so, you can easily use readtable().
fereshteh izadi
on 12 Feb 2016
Walter Roberson
on 12 Feb 2016
In MATLAB, please do
filecontent = fileread('file.txt');
first20 = filecontent(1:20);
first20
double(first20)
and show us the output. This will allow us to figure out details of how the file is stored.
fereshteh izadi
on 12 Feb 2016
Edited: fereshteh izadi
on 12 Feb 2016
per isakson
on 12 Feb 2016
In your question the column and row headers are identical. However, in the sample file, tRMA.txt, the column and row headers are NOT identical. In what kind of variable do you want the headers? (ND.m indicates that the numerical data shall be transferred to a double array.)
fereshteh izadi
on 12 Feb 2016
Edited: fereshteh izadi
on 12 Feb 2016
per isakson
on 12 Feb 2016
Edited: per isakson
on 13 Feb 2016
Yes, ND.m takes a square double array and no strings. Try the code in my answer. It should read files like tRMA.txt regardless of the number of columns and rows.
Accepted Answer
More Answers (2)
Walter Roberson
on 12 Feb 2016
fid = fopen('tRMA.txt, 'rt');
%All columns are tab separated, but there is no initial tab before the first gene row
%header which corresponds to the second column of input for the rest of the file,
%with the first column of input being a row name string
header = fgetl(fid);
col_names = regexp(header, '\t\, 'split');
num_cols = length(col_names);
fmt = ['%s', repmat('\t%f', 1, num_cols)];
datacell = textscan(fid, fmt, 'CollectOutput', 1, 'Delimiter', '\t');
fclose(fid);
row_names = datacell{1};
cor = datacell{2};
Now there is row_names (a cell array of strings), col_names (a cell array of strings), and cor (a rectangular numeric matrix)
A Mugesh
on 24 Apr 2019
0 votes
Hi,
I am a beginer in mat lab learning, i want to know the code how to read the different format of files in matlab....
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!

