필터 지우기
필터 지우기

Picking one line from a .txt file.

조회 수: 2 (최근 30일)
Ivan Mich
Ivan Mich 2019년 10월 1일
답변: meghannmarie 2019년 10월 1일
Hello,
I have a question for a matlab code. I have a .txt file which includes numbers and words (the first 10 lines includes words and from 11th line and above include numbers). I would like to take from this file only the 5th line. I have used these commands:
t=readtable('file.txt','Delimiter','\t','HeaderLines',1)
%t = your table;
dataCell = table2cell(t); % convert to cells
dataCell = dataCell(11:end); % remove unneccessary text
cellNums = cellfun(@str2num,dataCell,'UniformOutput',0); % apply str to num on all the data
output = cell2mat(cellNums); % convert to matrix with 2 columns.
Which commands should I use in order to take only the 5th line of this .txt file?
  댓글 수: 3
Ivan Mich
Ivan Mich 2019년 10월 1일
dadavad.png
Ajay Kumar
Ajay Kumar 2019년 10월 1일
Upload the txt file, not the image.

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

채택된 답변

meghannmarie
meghannmarie 2019년 10월 1일
Try this:
filename = 'file.txt'
linenum = 5;
fileID = fopen(filename);
C = textscan(fileID,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
fclose(fileID);
line5 = cell2mat(C{:});

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by