필터 지우기
필터 지우기

Reading columns of a .DAT file

조회 수: 13 (최근 30일)
Donrad
Donrad 2018년 1월 31일
답변: Donrad 2018년 1월 31일
Hi All, I'm new to MATLAB and for my data processing, I need to read my data (format=float)from a .DAT file. This file contains 17 columns with close to 4000 rows (based on my experimental condition the # of rows can change), but always the two first rows are text formats (parameter name and then its units). Now I need to know how can I extract data with headers column-wise? I want to read each column and then write each of them into an EXCEL file.
Thanks
P.S: I found exactly the same question here , but no useful answers were provided
I have attached an example file
  댓글 수: 1
Akira Agata
Akira Agata 2018년 1월 31일
Looking at your data file, it seems that Import Tool would be easy and simple way to read. Have you ever tried this tool?

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 31일
fid = fopen(v1_Out-a.dat', 'rt');
firstline = fgetl(fid);
varnames = regexp(firstline, '\t', 'split');
nvars = length(varnames);
units = regexp(fgetl(fid), '\t', 'split');
fmt = repmat('%f', 1, nvars);
data = textscan(fid, fmt, 'collectdata', 1);
fclose(fid);
datatable = array2table(data, 'VariableNames', varnames, 'VariableUnits', units);
writetable(datatable, 'NameOfExcelFile.xls')
This might possibly lose the information about units.
Note that for the purposes of excel, you could just convert all the tab characters to commas and call that a .csv file.

추가 답변 (1개)

Donrad
Donrad 2018년 1월 31일
Thanks, both, I used the combination of both answers.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by