reading a file in matlab
1 回表示 (過去 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
 
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 で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!