Reading in a *.dat file and replacing a part of it in the middle with new data

1 view (last 30 days)
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

Accepted Answer

Sachin Ganjare
Sachin Ganjare on 19 Oct 2012
You need to use fopen, fgetl, findstr functions.
Refer link below for example:
Hope it helps!!!

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!