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

3 ビュー (過去 30 日間)
Jeffrey Decker
Jeffrey Decker 2022 年 9 月 27 日
回答済み: dpb 2022 年 9 月 27 日
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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by