MATLAB: Tab delimited .txt file

text file

In order to write a matrix as a tab delimited .txt file I can use the following command:
dlmwrite(Filename, Data, 'delimiter', '\t');
Say that the dimensions of 'Data' is [m n], how is it possible to include a non numeric vector (i.e. vector containing words) as m(1) in order to state what each colummn refers to in the .txt file?
thanks

Best Answer

Suppose that your Data is:
Data = [1 2 3;4 5 6; 7 8 9];
Would you like to create such a file?
line1 1 2 3
line2 4 5 6
line3 7 8 9
If so, this is my suggestion:
Data = [1 2 3;4 5 6; 7 8 9];
names = {'line1','line2','line3'};
fid = fopen('test_file','w');
for k=1:3
fprintf(fid,'%s\t%.2f\t%.2f\t%.2f\n',names{k},Data(k,:));
end
fclose(fid);
Related Question