reading a file in matlab

5 vues (au cours des 30 derniers jours)
fereshteh izadi
fereshteh izadi le 12 Fév 2016
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 commentaires
fereshteh izadi
fereshteh izadi le 12 Fév 2016
Modifié(e) : fereshteh izadi le 12 Fév 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 le 12 Fév 2016
Modifié(e) : per isakson le 13 Fév 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.

Connectez-vous pour commenter.

Réponse acceptée

per isakson
per isakson le 12 Fév 2016
Modifié(e) : per isakson le 15 Fév 2016
  • 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 commentaires
per isakson
per isakson le 16 Fév 2016
Replace
colhead = strsplit( str, '\t' );
by
colhead = regexp( str, '\t', 'split' );
fereshteh izadi
fereshteh izadi le 16 Fév 2016
thank you very much, your code read correlation.txt well.

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 12 Fév 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
A Mugesh le 24 Avr 2019
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....

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by