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

32 views (last 30 days)
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
chocho
chocho on 24 Dec 2018
Edited: chocho on 24 Dec 2018
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)

Cris LaPierre
Cris LaPierre on 24 Dec 2018
Edited: Cris LaPierre on 24 Dec 2018
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
Cris LaPierre
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
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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!