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

2 views (last 30 days)
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)

Answers (1)

dpb
dpb on 27 Sep 2022
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

Community Treasure Hunt

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

Start Hunting!