Add leading decimal places to numbers in text file

1 次查看(过去 30 天)
I have data in a text file and would like to change some numbers while keeping the rest of the line the same.
For instance I have:
Name,proj2;
Edition,59;
Start;
Variable,1;
DataReg,0.00,0.00;
Area,520.00;
300,221.7467,424.1668,801.0146;
390,117.4175,507.8583,29.2203;
Area,530.00;
300,488.6090,963.0885,488.8977;
390,578.5251,546.8057,624.0601;
Datareg,00.00,30.00;
Area,520.00;
300,367.4366,913.2868,335.3568;
390,987.9820,796.1839,679.7280;
Area,530.00;
300,106.7619,715.0371,698.7458;
390,653.7573,903.7206,197.8098;
DataReg,00.00,160.00;
Area,520.00;
300,291.9841,167.1684,489.6876;
390,431.6512,106.2163,339.4934;
Area,530.00;
300,522.6770,5147.8709,3201.4549;
390,7137.8581,9142.7370,7101.0988;
DataReg,30.00,00.00;
Area,520.00;
300,2121.7467,4214.1668,8201.0146;
390,1217.4175,5107.8583,219.2203;
Area,530.00;
300,4188.6090,9163.0885,4288.8977;
390,5178.5251,5146.8057,6224.0601;
Datareg,30.00,30.00;
Area,520.00;
300,3167.4366,9113.2868,3135.3568;
390,9287.9820,7296.1839,6279.7280;
Area,530.00;
300,1106.7619,1715.0371,1698.7458;
390,1653.7573,1903.7206,1197.8098;
DataReg,30.00,160.00;
Area,520.00;
300,2921.9841,1672.1684,2489.6876;
390,4321.6512,1206.2163,2339.4934;
Area,530.00;
300,522.6770,57.8709,3021.4549;
390,7327.8581,9422.7370,7021.0988;
DataReg,160.00,0.00;
Area,520.00;
300,316.4366,911.2868,313.3568;
390,92.9820,729.1839,627.7280;
Area,530.00;
300,110.7619,171.0371,169.7458;
390,165.7573,190.7206,119.8098;
DataReg,160.00,30.00;
Area,520.00;
300,292.9841,167.1684,248.6876;
390,432.6512,120.2163,233.4934;
Area,530.00;
300,52.6770,5.8709,302.4549;
390,732.8581,942.7370,702.0988;
DataReg,160.00,160.00;
Area,520.00;
300,7292.9841,7167.1684,7248.6876;
390,7432.6512,7120.2163,7233.4934;
Area,530.00;
300,752.6770,75.8709,7302.4549;
390,7732.8581,7942.7370,7702.0988;
(Sorry it's so long, just wanted to paint a vivid picture)
Now, what I want to do is change the numbers in lines containing "DataReg" into the same numbers except with three leading spaces in front of decimal while still containing the two trailing decimal places. For example, I want to change this:
DataReg,0.00,0.00;
Datareg,0.00,30.00;
DataReg,0.00,160.00;
DataReg,30.00,0.00;
Datareg,30.00,30.00;
DataReg,30.00,160.00;
DataReg,160.00,0.00;
DataReg,160.00,30.00;
DataReg,160.00,160.00;
Into this:
DataReg,000.00,000.00;
Datareg,000.00,030.00;
DataReg,000.00,160.00;
DataReg,030.00,000.00;
Datareg,030.00,030.00;
DataReg,030.00,160.00;
DataReg,160.00,000.00;
DataReg,160.00,030.00;
DataReg,160.00,160.00;
Notice how some values (i.e.,160.00) already have three leading places and hence don't need change. keep in mind this will be for a large .txt file and other DataReg numbers such as 170.00, 180.00, 190.00 will exist, but nothing above 999.00 exists. Lots of comments please!

采纳的回答

dpb
dpb 2021-2-16
编辑:dpb 2021-2-16
Air code--caution!!!
c=readcell('YourTextFile.ext'); % bring file in as cellstr() array
isDR=startsWith(c,'DataReg'); % logical addressing array of wanted lines
fmt='DataReg,%07.3f,%07.3f;'; % desired output format
c(isDR)=compose(fmt,str2double(split(extractAfter(c(isDR),'DataReg,'),','))); % rewrite as wanted
writecell(c,'YourNewTextFile.ext','QuoteStrings',0) % put in new file (can overwrite original once sure is as wanted)
In short, don't try to fix up based on what is there by counting string lengths or such, just write out in the desired format the values that are there.
ADDENDUM:
Above patched to incorporate fixes outlined in Comments...the doc in R2019b is wrong on default behavior of quoted strings in "writecell'; and the release OP is using is same default behavior; whether the doc matches now or not, I don't know. Worthy of reporting the documentation error if doesn't.
  9 个评论
dpb
dpb 2021-2-17
编辑:dpb 2021-2-18
Oh. You're going to have same problem with writecell, too, then, it was also not introduced until R2019a.
There's no other way then but to revert to low-level i/o -- this has been an Achilles' heel problem for as long as have had cell arrays...
fid=fopen('YourFile.ext','w');
for r=1:size(c,1)
fprintf(fid,'%s\n',c{r});
end
fclose(fid);
Timbo
Timbo 2021-2-17
The work-arounds worked! Managed to make the changes I needed. Thank you kindly!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by