how to extract some specific rows and columns with their corresponding values from a text file with rows and column headers?
Show older comments
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
Walter Roberson
on 24 Dec 2018
We will need an example to be sure we understand.
Guillaume
on 24 Dec 2018
Why read the data into cell arrays and not tables which are designed exactly for this type of data?
chocho
on 24 Dec 2018
chocho
on 24 Dec 2018
chocho
on 24 Dec 2018
Walter Roberson
on 24 Dec 2018
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?
chocho
on 24 Dec 2018
Cris LaPierre
on 24 Dec 2018
Edited: Cris LaPierre
on 24 Dec 2018
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?
madhan ravi
on 24 Dec 2018
Edited: madhan ravi
on 24 Dec 2018
Also upload your textfile , zip it and upload .
chocho
on 24 Dec 2018
chocho
on 24 Dec 2018
madhan ravi
on 24 Dec 2018
Answers (1)
Cris LaPierre
on 24 Dec 2018
Edited: Cris LaPierre
on 24 Dec 2018
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
chocho
on 24 Dec 2018
Image Analyst
on 24 Dec 2018
Please edit your file to chop off all but around 10 or 20 rows. Then attach the shorter version with the paper clip icon.
chocho
on 24 Dec 2018
Cris LaPierre
on 24 Dec 2018
Edited: Cris LaPierre
on 28 Dec 2018
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.
chocho
on 24 Dec 2018
chocho
on 24 Dec 2018
chocho
on 24 Dec 2018
Cris LaPierre
on 24 Dec 2018
Edited: Cris LaPierre
on 24 Dec 2018
Works for me. What version of MATLAB are you using?
Try this:
T1 = readtable('textfile2.txt','ReadVariableNames',true,'ReadRowNames',true)
chocho
on 24 Dec 2018
Guillaume
on 24 Dec 2018
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.
chocho
on 24 Dec 2018
Image Analyst
on 24 Dec 2018
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).
Cris LaPierre
on 24 Dec 2018
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.
Guillaume
on 24 Dec 2018
"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.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!