reading a matrix in matlab

hello, I have a matrix like below
> head(mat[,1:4])
AT1G01060 AT1G01170 AT1G01180 AT1G01260
AT1G01060 1.0000000 0.3193360 0.6272994 0.2658624
AT1G01170 0.3193360 1.0000000 0.3178880 0.3588030
AT1G01180 0.6272994 0.3178880 1.0000000 0.2542234
AT1G01260 0.2658624 0.3588030 0.2542234 1.0000000
AT1G01380 0.6178751 0.1561297 0.7052692 0.3252033
AT1G01490 0.5990499 0.6129301 0.6424225 0.5727253
>
how I can read that as a matrix to run as an input of my code. I attached my code thank you for your help

1 Comment

You attached the code but not the file you are reading. Attaching ‘ND.m’ is not relevant because it apparently wants the matrix you want to read as its input argument.
I provided a textscan call to read your file (as best I could, considering I do not have your file to work with). Have you used it? Did it work?

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 11 Jan 2016
You did not attach your code. If this is a text file, please attach the file as well.
The easiest way to import it as a text file would be to use the textscan function.

6 Comments

<https://drive.google.com/file/d/0BwOX04QQkaXTVGoyOUhxM04tQUU/view if true % code end> sorry I attached my code.
I did like below but there is nothing is being read
>> fid = fopen('cor.txt', 'rt');
>> datacell = textscan(fid, '%s%s%d', 'HeaderLines', 1);
then how I can read my file to run in my code please??
Your format descriptor is wrong.
Use this:
datacell = textscan(fid, '%s%f%f%f%f', 'HeaderLines', 1, 'CollectOutput',1);
datamatrix = cell2mat(datacell);
You may need to include other name-value pairs (such as 'Delimiter' and perhaps 'EndOfLine') as well, depending on how your file is organised.
fereshteh izadi
fereshteh izadi on 12 Jan 2016
Edited: fereshteh izadi on 12 Jan 2016
google drive of my matrix thank you, I did like you suggested but I got this error >> fid = fopen('cor.txt', 'rt'); >> datacell = textscan(fid, '%s%f%f%f%f', 'HeaderLines', 1, 'CollectOutput',1); >> datamatrix = cell2mat(datacell); Error using cell2mat (line 46) All contents of the input cell array must be of the same data type. I attached the google derive link of my matrix. may you please consider and help to read my file? thank you so much
I cannot access that because I don’t have permission.
Attach it here using the ‘paperclip’ icon, and complete both the ‘Choose file’ and ‘Attach file’ steps.
sorry file is too big to be attached but as i pasted above, my file is a correlation matrix with diagonal=1 and rownames and colnames are my genes and nrow=ncol but i dont know how to read my file
See if this works:
Ngenes = 20; % Insert Correct Value Here
datacell = textscan(fid, ['%*s' repmat('%f',1,Ngenes)], 'HeaderLines', 1, 'CollectOutput',1);
datamatrix = cell2mat(datacell);
The '%*s' tells textscan to skip over the first (string) column. The repmat call duplicates the '%f' format descriptor for the number of genes that are in your correlation matrix. The cell2mat call should then work, since ‘datacell’ will have only numeric values.
I cannot test this without your file, but it should work.

Sign in to comment.

Tags

Asked:

on 11 Jan 2016

Commented:

on 12 Jan 2016

Community Treasure Hunt

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

Start Hunting!