reading a file in matlab
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
per isakson
2016-2-12
编辑:per isakson
2016-2-13
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.
采纳的回答
per isakson
2016-2-12
编辑:per isakson
2016-2-15
- 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
 
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172836/image.png)
 
The names of the rows and the columns are in the cell arrays, rowhead and colhead.
9 个评论
per isakson
2016-2-16
Replace
colhead = strsplit( str, '\t' );
by
colhead = regexp( str, '\t', 'split' );
更多回答(2 个)
Walter Roberson
2016-2-12
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)
0 个评论
A Mugesh
2019-4-24
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....
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!