reading a file in matlab

조회 수: 1 (최근 30일)
fereshteh izadi
fereshteh izadi 2016년 2월 12일
답변: A Mugesh 2019년 4월 24일
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
fereshteh izadi
fereshteh izadi 2016년 2월 12일
편집: fereshteh izadi 2016년 2월 12일
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 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
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
&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
per isakson
per isakson 2016년 2월 16일
Replace
colhead = strsplit( str, '\t' );
by
colhead = regexp( str, '\t', 'split' );
fereshteh izadi
fereshteh izadi 2016년 2월 16일
thank you very much, your code read correlation.txt well.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
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)

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

카테고리

Help CenterFile 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!

Translated by