MATLAB Answers

Automatically change header name

4 views (last 30 days)
I have an equation z=x+a*n. Here a is a constant, and x varies from 0 to 10 with 1 interval. Also, n varies from 0 to 10. I have to write all these on to a dat file with first column with all values of x, then next column with values of z with n=0, next column with values of z with n=1 and so on. And also I have to put a header. The code goes like this:
clear all;clc;format long;
a=4;
mat1=[];
for n=0:10
i=0;
for x=0:1:10
i=i+1;
z(i,:)= x+a*n;
end
mat1=[mat1,z];
end
x1(:,1)=0:1:10;
dat1=[x1, mat1];
outputfilename1=('mat1.dat');
fileidentifier1=fopen(outputfilename1,'w+');
fprintf(fileidentifier1,'x\t z0\t z1\t z2\t z3\t z4\t z5\t z6\t z7\t z8\t z9\t z10\t \r');
fprintf(fileidentifier1,'%7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f \n',dat1');
fclose(fileidentifier1);
Problem starts from putting header.
What I used to do is that I used to give the command for each of these thing manually: i.e. ‘x’, for first column, ‘z0’ for second column corresponding to n=0, ‘z1’ corresponding to n=1 and so on. As you can see that it will be a hectic of a task if that ‘n’ varies up to 100 or more. Also if you want to change the value of n from 10 to 9, the out put file will still show the correct values, but the way it is arranged will become haywire (you can try for yourself). Is there any way to write do this automatically?

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 3 Apr 2017
Edited: Stephen Cobeldick on 4 Apr 2017
>> fmt = ['x',sprintf('\\tz%d',1:10),'\n']
fmt = x\tz1\tz2\tz3\tz4\tz5\tz6\tz7\tz8\tz9\tz10\n
Also note that you should open a text file using the text t option, which will correctly interpret newlines in windows as \r\n and convert them during reading and writing (saving you a lot of hassle):
fid = fopen(...,'wt');
In fact your code could be simplified by removing the ugly loops:
% Generate Z values:
a = 4;
X = 0:10;
N = 0:6;
Z = bsxfun(@plus,X(:),a.*N);
% formats:
hdr = ['x',sprintf(',z%d',N),'\n'];
fmt = ['%d',repmat(',%d',1,numel(N)),'\n'];
% save data in file:
fid = fopen('temp2.txt','wt');
fprintf(fid,hdr);
fprintf(fid,fmt,[X;Z.']);
fclose(fid);
which gives this file (I used CSV, because this is a much better format than tab delimited or constant width):
x,z0,z1,z2,z3,z4,z5,z6
0,0,4,8,12,16,20,24
1,1,5,9,13,17,21,25
2,2,6,10,14,18,22,26
3,3,7,11,15,19,23,27
4,4,8,12,16,20,24,28
5,5,9,13,17,21,25,29
6,6,10,14,18,22,26,30
7,7,11,15,19,23,27,31
8,8,12,16,20,24,28,32
9,9,13,17,21,25,29,33
10,10,14,18,22,26,30,34

  2 Comments

sreeraj t
sreeraj t on 4 Apr 2017
Actually, my original equation is bit more complicated than simple equation like z=x+a*n. So i need to put for loop and all those stuff in it.
Also, if you change value of n from 10 to any other, say n=5, it is still printing up-to z10!
Stephen Cobeldick
Stephen Cobeldick on 4 Apr 2017
"if you change value of n from 10 to any other, say n=5, it is still printing up-to z10!"
See the edited answer, it correctly adjusts with the size of N.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!