Reading in a *.dat file and replacing a part of it in the middle with new data
2 次查看(过去 30 天)
显示 更早的评论
I want to generate some additional versions of a base *.dat file. In each additional version, I want to replace the data in "BLOCK 3". For all versions, the data in this block consists of one line with three numbers and then directly below it 63 lines with two numbers per line. Here is a piece of the base file:
*** BLOCK 1. unit system ***
1
*** BLOCK 2. structural data ***
1
9373.500
*** BLOCK 3. current data ***
63 0.0215529 025_000deg
0.0000 0.0000
0.0685 0.0055
0.0738 0.0053
*** BLOCK 4. s-n data ***
1
*** BLOCK 5. computation/output option ***
2
Here is what I have so far. I now have written the contents of the copy file up to the start of BLOCK 3. I know the identity of this line. Now what? I could write the new 64 lines in Block 3 from my mat file to the copy file using fprintf, but then what? I need to add the from the other blocks of the base file after that. Please help.
% Load the "base" dat file
fidbase = fopen('CASE_025_000deg.dat','r');
currentversion = ['CASE_' raw{2,3}];
fidcurrentversion = fopen([currentversion '.dat'],'wt');
count = 0;
while ~feof(fidbase)
count = count + 1;
line = fgetl(fidbase);
fwrite(fidcurrentversion,line);
fprintf(fidcurrentversion,'\n');
index = strfind(line,'BLOCK 3');
if ~isempty(index)
countblock3 = count;
break;
end
end
0 个评论
采纳的回答
Sachin Ganjare
2012-10-19
You need to use fopen, fgetl, findstr functions.
Refer link below for example:
Hope it helps!!!
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!