how to extract some specific rows and columns with their corresponding values from a text file with rows and column headers?
11 次查看(过去 30 天)
显示 更早的评论
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!
回答(1 个)
Cris LaPierre
2018-12-24
编辑:Cris LaPierre
2018-12-24
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 个评论
Cris LaPierre
2018-12-24
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
2018-12-24
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!