dlmwrite with a header

조회 수: 14 (최근 30일)
Erdem Uguz
Erdem Uguz 2016년 1월 9일
댓글: Turbulence Analysis 2020년 8월 20일
Hello,
I am trying to write data to a file. I have no problem just writing the file but I want the header positions to match the columns
dlmwrite([savepath,'/myFile.txt'],[]) % save path is the path defined before I create an %empty file first.
fid = fopen([savepath,'/myFile.txt'],'wt');
fprintf(fid, '%s\t %s\t %s\n', 'x','y1','y2'); % header
dlmwrite([savepath,'/myFile.txt'],MyData,'delimiter','\t','precision',['%10.',num2str(12),'f'],'-append'); % MyData is the file.
fclose(fid);
I would appreciate it if you can point out my mistake.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 9일
편집: Walter Roberson 2019년 11월 10일
You need to fclose(fid) before you dlmwrite() to the file. Also, you do not need to dlmwrite() it before you fopen() it.
filename = fullfile(savepath, 'myFile.txt');
fid = fopen(filename, 'wt');
fprintf(fid, '%s\t%s\t%s\n', 'x','y1','y2'); % header
fclose(fid);
dlmwrite(filename,MyData,'delimiter','\t','precision',['%10.',num2str(12),'f'],'-append');
  댓글 수: 8
Walter Roberson
Walter Roberson 2020년 8월 20일
What was the difficulty?
With the format you had, I do not see how you could have gotten the output you posted.
Turbulence Analysis
Turbulence Analysis 2020년 8월 20일
Hi, I did it in following way...
for k = 1:1:100
filename = sprintf('test%d.txt',k);
fid = fopen(filename,'w');
fprintf(fid, '#DaVis 8.4.0 2D-vector 8 92 112 "position" "mm" "position" "mm" "velocity" "m/s" \n');
fclose(fid);
end
for k = 1:1:100
UFF = X_filter(1:ni*nk,k);
VFF = X_filter(1+ni*nk:2*ni*nk,k);
UFF1 =reshape (UFF,ni,nk)';
VFF1 =reshape (VFF,ni,nk)';
UUFFF1 = reshape (UFF1, 10304,1);
VVFFF1 = reshape (VFF1,10304,1);
xxx = reshape (X,10304,1);
yyy = reshape (Y,10304,1);
fileName = sprintf('test%d.txt',k);
dlmwrite(fileName,[xxx yyy UUFFF1 VVFFF1],'delimiter','\t','-append');
end

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by