How do i extract specific row data whose value is changing from .dat file ?

조회 수: 2 (최근 30일)
Hi, I have a large .dat file (attached) and I am only interested in rows with specific values of constraint data of that redesing as shown below.
This is the data in .dat file as shown below:
$CONSTRAINT DATA NAME = D_Tstep1_OP_01
1.95063723e+00
$CHANGED TOPODRV NAME = M_OP_01
15932 2 1.77575504e-03 -3.88161658e-01
17231 2 1.80259062e-03 -3.91727572e-01
15291 2 -1.13913637e-02 2.21583925e+00
..
..
$CONSTRAINT DATA NAME = D_Tstep2_OP_01
3.95063723e+00
..
..
From the above .dat file, I only need data of D_Tstep1_OP_01 = 1.95063723e+00 with its values for 90 steps (like D_Tstep1_OP_01, D_Tstep2_OP_01,.......,D_Tstep90_OP_01). How do I extract the said data which has different delimiters from the .dat file and save it in excel? I need the data in the following format in excel:
NAME CONSTRAINT DATA
D_Tstep1_OP_01 1.95063723e+00
D_Tstep2_OP_01 3.95063723e+00
..
..
D_Tstep90_OP_01 5.95063723e+00 % this is the last value in the file
Your assistance is most appreciated. Thank you in advance.

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 9월 22일
hello
I wrote this little code for you !
check it
% main code
[ string_out, data_out ] = retrieve_data('Redesign2-0_OP_01.dat');
C = [{' NAME '} {'CONSTRAINT DATA'} ;string_out' data_out'];
writecell(C, 'C_out.xlsx');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ string_out,data_out ] = retrieve_data(Filename)
fid = fopen(Filename);
tline = fgetl(fid);
% initialization
k = 0;
p = 0;
data_line = [];
while ischar(tline)
k = k+1; % loop over line index
tmp1 = contains(tline,'$CONSTRAINT DATA NAME = D_TStep');
if tmp1 && ~isempty(tline)
p = p+1;
ind_eq = strfind(tline,'=');
string_out{p} = (tline(ind_eq+2:end)); %
data_line = k+1;
end
if ~isempty(data_line) && k == data_line
data_out{p} = str2num(tline); %
end
tline = fgetl(fid);
end
fclose(fid);
end
  댓글 수: 7
Shubham Gorde
Shubham Gorde 2021년 9월 27일
Thank you so much, I will implement it:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by