Overwriting Data in Text File with Content from Matlab Table, sample code attached!

1 次查看(过去 30 天)
Hi Community, the following problem occured to me:
I have a .txt file with looks like:
Name: Drifter #92
Note: No notes
Serial Number#: 1
Address: 92
Accumulation of samples: 1
The measurement period: 10
Latitude: 0.000000
Longitude: 0.000000
Time UTC Water Air Chip
15.07.2015 12:24:47 +107.117 +106.925 +17.179 ...
15.07.2015 12:24:52 +107.116 +106.926 +17.124 ...
15.07.2015 12:24:57 +107.115 +106.935 +17.167 ...
15.07.2015 12:25:02 +107.116 +106.931 +17.131 ...
15.07.2015 12:25:07 +107.115 +106.924 +17.125 ...
15.07.2015 12:25:12 +107.113 +106.930 +17.107 ...
.
.
.
Due to speed issues I load only the first three columns into Matlab Workspace as a table, like this:
15.07.2015 12:24:47 +107.117 +106.925
15.07.2015 12:24:52 +107.116 +106.926
15.07.2015 12:24:57 +107.115 +106.935
15.07.2015 12:25:02 +107.116 +106.931
15.07.2015 12:25:07 +107.115 +106.924
15.07.2015 12:25:12 +107.113 +106.930
Now I calculate a little and change the date format:
2015-07-15 12:24:47 17.70681925195 17.0387490668112
2015-07-15 12:24:47 17.7042602411612 17.041305748443
2015-07-15 12:24:47 17.7017012303724 17.064315883129
2015-07-15 12:24:47 17.7042602411612 17.0540891566019
2015-07-15 12:24:47 17.7017012303724 17.0361923851795
2015-07-15 12:24:47 17.6965832087948 17.0515324749701
This data is saved in a cell array, where the first cell content is of type "string" and the others of type "double.
Finally I would like to replace the top three columns in the original textfile by the newly calculated ones, without overwriting the whole textfile.
I tried a lot with pointers to get to the right line in the textfile before wrting to it, but I don't come to a smart solution.
An exemplary .txt file, running sample code plus an import function generated by Matlab are attached.
Thanks for your help! With best regards, Helge.

采纳的回答

Guillaume
Guillaume 2015-9-11
With the example you've shown what you're asking is not possible. The only way you could do what you want is if the replacement text has exactly the same number of characters as the one you replace. If it has less, you can add spaces to make it equal, but if it has more, like in your example, it's not physically possible as the rest of the file needs to be moved to make room for the extra characters. Hence you've got to rewrite everything after the replacement.
If you're willing to keep the formatting of the water and air column as it is originally, and assuming the header is always the same number of lines, then:
fid = fopen('somefile.txt', 'rt+')
for headerline = 1:10
fgetl(fid); %skip header lines
end
for row = 1:size(newdata, 1) %assuming your cell array is newdata
startline = ftell(fid); %get current position
fseek(fid, 0, 'cof'); %required because switching from reading to writing
fprintf(fid, '%s\t% +7.2f\t%+7.2f', newdata{row, 1:3}); %the format string assumes no value is above 999.99
fseek(fid, startline, 'bof'); %return to beginning of line
fgetl(fid); %read whole line to position file pointer to beginning of next line
end
Code is untested, there may be bugs in the formatting string, number of header lines, etc.
  2 个评论
Helge
Helge 2015-9-11
编辑:Helge 2015-9-11
Hey, thanks for your fast and explanatory answer :)
Luckily I can assume, that the header and the overall format will always be the same and I dont require all digits. With some little changes I was finally able to make my code run. Updated SampleCode is attached.
Thanks a lot and a nice weekend! Greetings
Mohamed Noohu Lebbai
Hi Helge & Guillaume,
Request your support.I already posted long back in community
I am also facing the same difficulty in overwriting .m file(File2.m) with calculated matlab table(Cal.mat) by writing script in File3.m
Explanation:
1.I need to write a script in File3.m.
2.I need to open the File2.m (By path)
3.I need to overwrite the File2.m file's A1(2:4) based on cal(19:21) every time.
4.I need to save the File2.m
Thanks in advance
Noohu.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by