how to extract some specific rows and columns with their corresponding values from a text file with rows and column headers?

Hello guys,
I have a text file with 219 rows and 12586 columns and with row and column headers.
i want to read this textfile into a cell array with the headers too
I want to extract some rows and columns from this textfile based on 2 cell arrays, cell1 (of size 9*1) its content exist in the rows of this text file and cell 2 (321*1) its content exist in the columns of this textfile.
How to compare them ?
i tried to use textscan, import data and readttable but no one work properly
I appreciate anyhelp plz!

14 Comments

Why read the data into cell arrays and not tables which are designed exactly for this type of data?
@Guillaume bcz i want to do some analysis with only some rows and columns which are easy to handle with cell arrays rather than table, i'm right ?
@Walter Roberson The textfile exceeds 5MB so i attached an img of only some rows and columns from it
Is cell1 intended to match only items in column 1?
What would be some examples of entries in cell2? Are exact matches being looked for? Are the entries in cell2 variable names?
@Walter Roberson Yes Walter, cell1 contents are all existed in textfie and cell2 contents are gene names and are also exist in textfile and my goal to get them with their corresponding values and the output will like a matrix of those 9 rows and those 321 columns with their values from textfile.
I would use readtable to load the data. Set the column names as variable names, and set the rownames to be whatever is in rows.mat. You can then index using row and variable names. Could you explain why it didn't work? Or share the code you tried and any error messages you got?
Also upload your textfile , zip it and upload .
i tried to uplaod the text file but is said it exceeds 5 MB here
I did that but show me the same error Cannot attach this file because:
  • File:X_patho_Expression.zip size exceeds 5 MB. Try compressing the image file in an archive format; for example, a zip file.
Data = readtable('textfile.txt','ReadVariableNames',true);
fidexpw= fopen('textfile.txt','rt');
format = repmat('%q',[1 12587]);
expression = textscan(fidexpw,format,'Delimiter','\t ');
fclose(fidexpw);
xx = importdata('textfile.txt','\t');
x = xx.data;
This is what i all tried

Sign in to comment.

Answers (1)

For loading the data, see the examples on this page.
For accessing the data from the table, see the examples on this page.
I would do something like this (the dataset is included as an example in MATLAB, so you can run this, too):
filename = fullfile(matlabroot,'examples','matlab','myCsvTable.dat');
T1 = readtable(filename,'ReadRowNames',true);
T1 =
Gender Age Height Weight Smoker
______ ___ ______ ______ ______
Smith 'M' 38 71 176 1
Johnson 'M' 43 69 163 0
Williams 'F' 38 64 131 0
Jones 'F' 40 67 133 0
Brown 'F' 49 64 119 0
T2 = T1({'Williams','Jones'},{'Height','Weight','Age'})
T2 =
Height Weight Age
______ ______ ___
Williams 64 131 38
Jones 67 133 40

14 Comments

Please edit your file to chop off all but around 10 or 20 rows. Then attach the shorter version with the paper clip icon.
Sorry. I understood from what you were posting that that was how you wanted to extract your data. You can also extract using indexing.
T3 = T1(3:4,[3 4 2])
T3 =
Height Weight Age
______ ______ ___
Williams 64 131 38
Jones 67 133 40
Look at the links, as they provide all the options.
@Image Analyst i have attached the data named textfile2.txt in the original thread
@Cris LaPierre i attached the data can you have a look plz ?textfile2.txt
Appreciated you help!
@Cris LaPierre this command doesn't work even it shows me a table of 0*0
T1 = readtable('textfile.txt','ReadRowNames',true);
Works for me. What version of MATLAB are you using?
Try this:
T1 = readtable('textfile2.txt','ReadVariableNames',true,'ReadRowNames',true)
You've asked enough questions here that by now you should know that "it doesn't work" is useless to us. If you get an error then give us the full text of the error message. If it doesn't produce the result you wanted then how is it different from what you wanted.
@Guillaume i just said what i tried.
Ok Thanks and sorry for the multiple questions!
Just look at the documentation for readtable() and see what options exist in your version. Maybe your version doesn't have ReadVariableNames' and 'ReadRowNames' and you'll have to make adjustments. You can do that better than us because we're all on more recent versions and cannot go back (at least not easily).
I installed 2015a to test this out. In this version, you still have to specify the delimiter. Try this:
T1 = readtable('textfile2.txt','ReadVariableNames',true,'ReadRowNames',true,'Delimiter','\t')
It looks like the variable editor is not able to generate a preview of this variable in this version, but if I sample specific points, it appears to be correct. You can print to the screen if you want to verify.
"i just said what i tried."
Yes, but you didn't say why it didn't work, so we're left playing the guessing game. Maybe it didn't work because the file is too complicated for these functions? Maybe it didn't work because you needed another option for readtable. Or maybe it didn't work because the file can't be found (we seen that several times) or o locked by another process or maybe even because you computer isn't on.

Sign in to comment.

Categories

Products

Release

R2015a

Tags

Asked:

on 24 Dec 2018

Edited:

on 28 Dec 2018

Community Treasure Hunt

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

Start Hunting!