How to change the format of input .txt files?

Hello,
I have a question about a code. I have multiple .txt files with a specific format. Specifically , exh file has 1 header, and 4 columns with 1 tab space between each other (the 4th column has an alphanumeric format, and columns 1-3 are numbers).
I would like to change the given format, which is f7.3 1x f7.3 1x f3.1 1x character to the following
1x f6.3 4x f6.3 4x 3.1 1x character (x is tab (space) and f is float (real) number).
I am attaching an input file as an example...
Could you please help me?

답변 (1개)

Walter Roberson
Walter Roberson 2022년 6월 8일

0 개 추천

You may have to read the header separately. Then readtable. Then I suggest you use compose() passing in a string() object as the format. Then write the strings to a file, possibly strjoin and fwrite

댓글 수: 10

Ivan Mich
Ivan Mich 2022년 6월 8일
편집: Ivan Mich 2022년 6월 8일
I am using the following code
clc
clear
filename1= 'input_file_test.txt';
d1=readtable(filename1);
formatSpec=" %6.3d %6.3d %3.1d %d"
str=compose(formatSpec,d1)
but command window shows :
Error using compose (line 98)
Unable to convert 'table' value to 'int64'.
Error in Untitled (line 9)
str=compose(formatSpec,d1)
How can I fix it?
filename1 = 'input_file_test.txt';
d1 = readtable(filename1, 'ReadVariableNames', false);
formatSpec = "\t%6.3d\t\t\t\t%6.3d\t\t\t\t%3.1d\t%s"
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, d1{:,4});
ss = strjoin(str, "\n");
fid = fopen(OutputFile, 'w') ;
%write any header at this point
%then
fwrite(fid, ss) ;
fclose(fid) ;
Ivan Mich
Ivan Mich 2022년 6월 8일
thank you but there is a problem...command window shows me
Error using compose
Function is not defined for 'cell' inputs.
Error in Untitled (line 7)
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, d1{:,4});
Is there something else to try ?
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, string(d1{:,4}));
Ivan Mich
Ivan Mich 2022년 6월 8일
편집: Ivan Mich 2022년 6월 9일
I am sorry but it is no use..
command window still show:
Error using compose
Function is not defined for 'cell' inputs.
Error in Untitled (line 7)
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, string(d1{:,4}));
Could you please help me?
Ivan Mich
Ivan Mich 2022년 6월 9일
편집: Ivan Mich 2022년 6월 9일
I have also tried
clc
clear
filename1 = 'input_file_test.txt';
d1 = readtable(filename1, 'ReadVariableNames', false);
formatSpec = "\t%6.3d\t\t\t\t%6.3d\t\t\t\t%3.1d\t%s";
str = compose(formatSpec, d1(:,1), d1(:,2), d1(:,3), string(d1{:,4}));
ss = strjoin(str, "\n");
fid = fopen(OutputFile, 'w') ;
%write any header at this point
%then
fwrite(fid, ss) ;
fclose(fid) ;
but it is not working, as command window shows me
Error using compose (line 98)
Unable to convert 'table' value to 'int64'.
Error in Untitled (line 8)
str = compose(formatSpec, d1(:,1), d1(:,2), d1(:,3), string(d1{:,4}));
Could you please help me?
I tested the following on my system.
filename1 = 'input_file_test.txt';
OutputFile = 'output_file_test.txt';
d1 = readtable(filename1, 'ReadVariableNames', false);
formatSpec = "\t%6.3d\t\t\t\t%6.3d\t\t\t\t%3.1d\t%s"
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, string(d1{:,4}));
ss = strjoin(str, newline);
fid = fopen(OutputFile, 'w') ;
%write any header at this point
%then
fwrite(fid, ss) ;
fclose(fid) ;
Ivan Mich
Ivan Mich 2022년 6월 10일
Thank you @Walter Roberson for your help, but it is no use. Command window shows me the same message:
Error using compose
Function is not defined for 'cell' inputs.
Error in Untitled (line 9)
str = compose(formatSpec, d1{:,1}, d1{:,2}, d1{:,3}, string(d1{:,4}));
I am supposing that problem is in "compose" command...
Please show
class(d1{:,1})
class(d1{:,2})
class(d1{:,3})
class(d1{:,4})
class(string(d1{:,4}))
Also please remind us while matlab version you are using.
well
ans =
'cell'
ans =
'cell'
ans =
'cell'
ans =
'cell'
ans =
'string'
I am using 2019a matlab version...

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2022년 6월 8일

댓글:

2022년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by