When converting a .txt file to a matrix/cell, MATLAB merges some of the columns together.
3 次查看(过去 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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!