필터 지우기
필터 지우기

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

조회 수: 1 (최근 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

채택된 답변

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by