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
댓글 수: 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!