필터 지우기
필터 지우기

fix content .txt file (organize the inside of the .txt file)

조회 수: 8 (최근 30일)
Alberto Acri
Alberto Acri 2023년 6월 16일
편집: dpb 2023년 6월 18일
Hi! How can I transform the file data.txt to data_new.txt?

답변 (2개)

dpb
dpb 2023년 6월 16일
편집: dpb 2023년 6월 17일
You can try
tYourTable=renamevars(tYourTable,{'Parameters1','Parameters2'},{'Parameters:','Values'});
writetable(tYourTable,'YourTableOutput.txt','Delimiter','\t','WriteVariableNames',1,'QuoteStrings',0)
Given the variable width of the text column data, it's likely the tab spacing won't show up with evenly aligned columns unless you use an application to view it that can set tab spacing wider than the default 8 characters most apps use.
If the above isn't of sufficient beauty, you'll be forced to use low-level i/o with fprintf and fixed-width columns of the needed/desired size.
  댓글 수: 2
Alberto Acri
Alberto Acri 2023년 6월 17일
Thank you for your response! I made a change to the post.
dpb
dpb 2023년 6월 17일
편집: dpb 2023년 6월 17일
Same way; use tab-delimiter it you want a tab-delimited file when you create the first one.
If you want a fixed-width file instead, there's no builtin higher level function in MATLAB to do that; the closest would be compose although it hasn't been expanded to handle tables.
Trying to edit sequential text files in place programmatically is not the way to approach it; write the file as you want it to begin with.
And, reassess the need; what's the point other than pretty? Unless it is to be a formal report or the like, it may well not be worth the effort; if it's only to be an intermediary as input to another code, the code won't care what it looks like.

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


dpb
dpb 2023년 6월 18일
편집: dpb 2023년 6월 18일
As noted before, don't try to fix a malformed text file; create the file correctly in the first place...we'll start from the original to get the data, but unless this is the initial starting point, don't create a comma-delimited file at all, use writetable to output the initial file from the MATLAB table.
OTOH, if the initial file is what was provided by some other process and can't change that process, then
c=readcell('data.txt')
c = 2×3 cell array
{'data 1'} {'data 2'} {'data 3'} {[ 50]} {[ 140]} {[ 36]}
writecell(c,'data1.txt','delimiter','\t')
type data1.txt
data 1 data 2 data 3 50 140 36

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by