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 Kommentare

Ken Atwell
Ken Atwell am 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
fereshteh izadi am 12 Feb. 2016
thank you, please help me...
Image Analyst
Image Analyst am 12 Feb. 2016
Bearbeitet: Image Analyst am 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().
this file is a correlation matrix derived from R function cor
cor(file.txt) file is too big to be attached but i attached the file i produced this file by cor function I should read this file then I could run this matlab code but I never could read the file
> head(mycounts[,1:6])
AT1G01060 AT1G01170 AT1G01260 AT1G01380 AT1G01490 AT1G01500
AT1G01060 1.00000000 0.3885284 -0.14720327 -0.01865947 0.24681279 0.05720335
AT1G01170 0.38852841 1.0000000 -0.29069241 0.26992353 0.39557397 -0.17866996
AT1G01260 -0.14720327 -0.2906924 1.00000000 0.30973373 -0.07497821 -0.26813919
AT1G01380 -0.01865947 0.2699235 0.30973373 1.00000000 0.23271890 -0.60440141
AT1G01490 0.24681279 0.3955740 -0.07497821 0.23271890 1.00000000 -0.11413300
AT1G01500 0.05720335 -0.1786700 -0.26813919 -0.60440141 -0.11413300 1.00000000
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
fereshteh izadi am 12 Feb. 2016
Bearbeitet: fereshteh izadi am 12 Feb. 2016
thank you, i did like so
>> first20 = filecontent(1:20);
>> first20
first20 =
AT1G01060 AT1G01170
>> double(first20)
ans =
Columns 1 through 13
65 84 49 71 48 49 48 54 48 9 65 84 49
Columns 14 through 20
71 48 49 49 55 48 9
>>
per isakson
per isakson am 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
fereshteh izadi am 12 Feb. 2016
Bearbeitet: fereshteh izadi am 12 Feb. 2016
yes tRMA.txt is a transposed matrix of my expression data sets that by cor function I derived a correlation matrix with identical column and row headers as an input for ND.m matlab code but I can;t read my file sorry by " ND.m indicates that the numerical data shall be transferred to a double array", you mean ND.m needs a two dimential matrix without column or row name??? if so then after running the code on a numerical double array how i can assign column and row name to the output of ND.m????
per isakson
per isakson am 12 Feb. 2016
Bearbeitet: per isakson am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

per isakson
per isakson am 12 Feb. 2016
Bearbeitet: per isakson am 15 Feb. 2016

1 Stimme

  • I failed to read tRMA.txt with Import Data. It choked Matlab (R2013a)
  • The code below reads the file, tRMA.txt.
  • I was a little surprised to see that num isn't square.
fid = fopen('tRMA.txt');
str = fgetl( fid );
[~] = fclose( fid );
colhead = strsplit( str, '\t' );
ncolumn = length( colhead );
fid = fopen('tRMA.txt');
fmt = ['%s',repmat('%f',[1,ncolumn])];
cac = textscan( fid, fmt, 'Headerlines',1, 'Delimiter','\t', 'CollectOutput',true );
[~] = fclose( fid );
rowhead = cac{1,1};
num = cac{1,2};
whos colhead rowhead num
outputs
Name Size Bytes Class Attributes
colhead 1x2885 375050 cell
num 164x2885 3785120 double
rowhead 164x1 22632 cell
&nbsp
In responce to comments:
I have converted the script to a function, preND. Functions are easier to use than scripts. Run
>> [ M, colhead, rowhead ] = preND( 'correlation.txt' );
>> mat_nd = ND( M );
>> imagesc( mat_nd )
where
function [ M, colhead, rowhead ] = preND( filespec )
fid = fopen( filespec );
str = fgetl( fid );
[~] = fclose( fid );
colhead = strsplit( str, '\t' );
ncolumn = length( colhead );
fid = fopen( filespec );
fmt = ['%s',repmat('%f',[1,ncolumn])];
cac = textscan( fid, fmt, 'Headerlines',1 ...
, 'Delimiter','\t', 'CollectOutput',true );
[~] = fclose( fid );
rowhead = cac{1,1};
M = cac{1,2};
end
the result is
&nbsp
The names of the rows and the columns are in the cell arrays, rowhead and colhead.

9 Kommentare

fereshteh izadi
fereshteh izadi am 14 Feb. 2016
thank you so much you both for your kindly attention. actually I should admit that I am too weak to adapt your code on my data and I got error. then I attached my file which ND.m code needs as input. as you mentioned already this code needs a double numerical array. my question is, how I can retrieve back my row names and colnames after running ND.m code????? you suppose I used the below syntax
M = dlmread('correlation.txt','\t')
mat_nd=ND(M,varargin)
it gives me an output but without rownames and colnames. then how i can have an output with the same header in my input correlation.txt??? please find my attached input file thanks again
per isakson
per isakson am 15 Feb. 2016
Bearbeitet: per isakson am 16 Feb. 2016
  • "adapt your code on my data and I got error" &nbsp You make it difficult to help! You don't tell what you do or what error you encounter. I cannot guess! And why did you upload tRMA.txt?
  • I cannot believe that &nbsp M = dlmread('correlation.txt','\t') &nbsp reads the file, which you uploaded together with your comment. Not with R2013a anyhow.
&nbsp
  • Here &nbsp mat_nd=ND(M,varargin) returns
&nbsp
  • "how I can retrieve back my row names and colnames after running ND.m code?????" &nbsp ND is totally ignorant of the row and column names; the names are neither in the input nor output of ND. Furthermore, dlmread cannot read the names - afaik.
fereshteh izadi
fereshteh izadi am 15 Feb. 2016
Bearbeitet: fereshteh izadi am 15 Feb. 2016
Dear per isakson, many thanks I performed as you commented but I got error by reading correlation.txt
let me ask an obvious question: how I can read correlation.txt in matlab??? I never could read this file in matlab and when i removed the colnames and rownames, by
M = dlmread('correlation.txt','\t')
i could read that...how i can read that when it is as i attached as correlation.zip???
actually I used a file without header as input for ND, I mean in R using rownames and colnames = FALSE, I removed the header of correlation.txt then i used as input. my question is; when ND gives me an output, is there anyway to back the header from the original correlation.txt(before removing the header) to the output ????
*[ M, colhead, rowhead ] = preND( 'correlation.txt' );
Undefined function 'preND' for input arguments of type 'char'.*
per isakson
per isakson am 15 Feb. 2016
Bearbeitet: per isakson am 15 Feb. 2016
"how I can read correlation.txt in matlab???" &nbsp By the following steps
  1. copy&paste the definition of the function, preND, from my answer to the Matlab editor (or any editor)
  2. save the function to a file named, preND.m, in the folder where you have ND.m
  3. put a copy of correlation.txt in the same folder
  4. run [M,colhead,rowhead] = preND('correlation.txt');
  5. if that fails, make sure that the folder is in the Matlab path.
"is there anyway to back the header from the original correlation.txt" &nbsp preND already did that (see my answer). However, how do you want to use all those names?
fereshteh izadi
fereshteh izadi am 15 Feb. 2016
Bearbeitet: Walter Roberson am 15 Feb. 2016
thank you for your patience
>> pwd
ans =
C:\Users\Lenovo\Desktop\RMA
>> ls
. GENEI3.R RMA.txt completecorrelation.txt evaluate.r silencing.txt
.. Genes_in_GoldST.txt SILENCING.m correlation.txt ggm.r silencing1.txt
Ara_GoldST.txt ND.m big.txt dream.txt preND.m test.r
>> [M,colhead,rowhead] = preND('correlation.txt');
Undefined function 'strsplit' for input arguments of type 'char'.
Error in preND (line 6)
colhead = strsplit( str, '\t' );
>>
Walter Roberson
Walter Roberson am 15 Feb. 2016
strsplit was introduced in R2013a. Which MATLAB version are you using?
R2012a
per isakson
per isakson am 16 Feb. 2016
Replace
colhead = strsplit( str, '\t' );
by
colhead = regexp( str, '\t', 'split' );
fereshteh izadi
fereshteh izadi am 16 Feb. 2016
thank you very much, your code read correlation.txt well.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 12 Feb. 2016

1 Stimme

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
A Mugesh am 24 Apr. 2019

0 Stimmen

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....

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by