Automatically change header name
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Import, Export, and Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!