필터 지우기
필터 지우기

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!
  댓글 수: 14
chocho
chocho 2018년 12월 24일
편집: chocho 2018년 12월 24일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2018년 12월 24일
편집: Cris LaPierre 2018년 12월 24일
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
Cris LaPierre
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
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by