dlmwrite with a header
Show older comments
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.
Answers (1)
Walter Roberson
on 9 Jan 2016
Edited: Walter Roberson
on 10 Nov 2019
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 Comments
Sunny Lightman
on 4 Nov 2019
not work for me ... sorry
Walter Roberson
on 10 Nov 2019
I made a minor correction.
Turbulence Analysis
on 19 Aug 2020
Hi Walter,
Hi,
I need to insert below header file in the first row of the each txt file, please assist me in how to do this..
#DaVis 8.4.0 2D-vector 12 62 75 "position" "mm" "position" "mm" "velocity" "m/s"
I tried something like this, but didn't work.. Please help me with this....
for j =1:1:2
fileName = sprintf('test%d.txt',j);
fid = fopen(filename,'w');
fprintf(fid, 'HEADER 1 \n');
fclose(fid);
end
for k = 1:1:2
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, 4650,1);
VVFFF1 = reshape (VFF1,4650,1);
xxx = reshape (X,4650,1);
yyy = reshape (Y,4650,1);
fileName = sprintf('test%d.txt',k);
dlmwrite(fileName,[xxx yyy UUFFF1 VVFFF1], 'delimiter','\t' );
end
Walter Roberson
on 19 Aug 2020
I do not think it is profitable to fight with dlmwrite() for something like this.
for k =1:1:2
fileName = sprintf('test%d.txt',k);
fid = fopen(filename,'w');
fprintf(fid, '%s\n', '#DaVis 8.4.0 2D-vector 12 62 75 "position" "mm" "position" "mm" "velocity" "m/s"');
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)';
fprintf(fid, '%.17g\t%.17g\t%.17g\%.17g\n', [X(:) Y(:) UFF1(:) VFF1(:)].'); %transpose is important!
fclose(fid);
end
You can adjust the %.17g as needed. %.17g should be enough that the values can be exactly reconstructed.
Question: are any of those variables complex-valued? Complex values need additional work. And your use of the conjugate transpose operator ' becomes questionable for complex values.
Turbulence Analysis
on 20 Aug 2020
Many thanks for your reply. I did tried with this, Actually I supposed to get the values as arranged in test1.txt, however, I am getting different as shown in teest1.tx, as you see everything arranged in a row..
I modified bit, bcoz i need to have data arranged in 4650 rows and 4 columns in txt file..
for k =1:1:3
fileName = sprintf('teest%d.txt',k);
fid = fopen(fileName,'w');
fprintf(fid, '%s\n', '#DaVis 8.4.0 2D-vector 12 62 75 "position" "mm" "position" "mm" "velocity" "m/s"');
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, 4650,1);
VVFFF1 = reshape (VFF1,4650,1);
xxx = reshape (X,4650,1);
yyy = reshape (Y,4650,1);
fprintf(fid, '%.17g\t%.17g\t%.17g\%.17g\n', [xxx(:) yyy(:) UUFFF1(:) VVFFF1(:)].'); %transpose is important!
fclose(fid);
end
Turbulence Analysis
on 20 Aug 2020
Hi, I solved the issue.. It's fine now..
Walter Roberson
on 20 Aug 2020
What was the difficulty?
With the format you had, I do not see how you could have gotten the output you posted.
Turbulence Analysis
on 20 Aug 2020
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
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!