how to extract number from a .dat file and assign it to an excel sheet cell?

조회 수: 3 (최근 30일)
I am using Fluent to extract surface integral values that are saved with a significant amount of text and the required numerical value. It looks like the following :
"Surface Integral Report"
Average of Surface Vertex Values
Wall Shear Stress (pascal)
-------------------------------- --------------------
wall 0.047597805
I would like to write a code to extract this numerical value, '0.047597805' from above .dat file and save it into a cell in an excel sheet ('.csv' or '.xlsx'). Please help!!

채택된 답변

Bhaskar R
Bhaskar R 2020년 2월 3일
편집: Bhaskar R 2020년 2월 3일
Assuming your a.dat file is with data
"Surface Integral Report"
Average of Surface Vertex Values
Wall Shear Stress (pascal)
-------------------------------- --------------------
wall 0.047597805
Then your code
T = readtable('a.dat'); % read data to table
T.Properties.VariableNames = {'Wall_Shear_Stress', 'pascal'}; % modify variable names
writetable(T, 'Reposrt.csv'); % write to csv/excel file
  댓글 수: 3
Bhaskar R
Bhaskar R 2020년 2월 3일
편집: Bhaskar R 2020년 2월 3일
num_files = <number of .dat files you want to put in loop>
data = zeros(num_files, 1); % matrix is recommended instead of cell as require
for ii =1:num_files
file_name = <process your file path here > % e.g: file_name = fullfile(folder_path, sprintf('a%d.dat',ii)) assuming your filenames as a1.dat, a2.dat, a3.dat...
ext_data = readmatrix(file_name) % read file
data(ii) = ext_data(2); % first value you would get NaN beacuse of text field
end
writematrix(data, 'Reposrt.csv' ) % write data to csv file
Adarsh Vasa
Adarsh Vasa 2020년 2월 3일
Thank you so much. It worked like a beauty. I got late in responding as I had to upgrade to the 2019 version, in order to use readmatrix function. Thanks again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by