필터 지우기
필터 지우기

When converting a .txt file to a matrix/cell, MATLAB merges some of the columns together.

조회 수: 1 (최근 30일)
I have an data file that is automatically saved as a .txt file. This .txt file inlcudes extraneous rows of unnecessary data that are irrelevant and need to be deleted. The .txt file is 504x10. My first thought was to convert this .txt file into a matrix using the "writematrix" function and delete the rows from the produced matrix, however when doing this, MATLAB merges multiple columns together and creates a matrix that is 498x2.
Below is the code that I have used.
C = readcell ('filename.txt');
writematrix ('filename.txt');
C ([1:40], :) =[]; % Delete rows 1-40 from the produced matrix (these are the rows that contain the unnecessary data)

답변 (1개)

dpb
dpb 2022년 9월 27일
C = readcell ('filename.txt');
writematrix ('filename.txt');
C ([1:40], :) =[]; % Delete rows 1-40 from the produced matrix (these are the rows that contain the unnecessary data)
Doesn't accomplish anything good on the disk file -- it retains a reduced C array in memory, but the disk file would have been wiped if it didn't error for lack of input arguments.
As @Stephen23 says, to retain the structure of the file as is, use read/writelines -- unfortunately, and I do not understand why, the 'NumberHeaderLines' parameter has not been implemented with readlines so you would have to write
yourfilename='filename.txt'; % don't mix data and code; use variables fullfile() would be good here, too
L=readlines(yourfilename);
L(1:40=[];
writelines(L,yourfilename)
Depending on other needs/desires, one might also consider the 'EmptyLineRule' parameter

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by