With minimal changes to what you have (change space to tab too)
tests = vertcat('CT4H12X9','CT5H12X9','CT5H12X8','CT6H12X7','CT7H12X2','CT8H12X2','CT7H12X5','CT8H12X6');
y_acc_max = rand(1,8);
fileID = fopen('acc_max.txt','w');
fprintf(fileID,'%8s\t%12s\r\n','Test','Maximum acceleration');
for irow = 1:length(y_acc_max)
fprintf(fileID,'%8s\t%12.8f\r\n',tests(irow,:),y_acc_max(irow));
end
fclose(fileID);
Another option with minimal change
tests = {'CT4H12X9','CT5H12X9','CT5H12X8','CT6H12X7','CT7H12X2','CT8H12X2','CT7H12X5','CT8H12X6'};
y_acc_max = rand(1,8);
A = [tests; num2cell(y_acc_max)]
fileID = fopen('acc_max_2.txt','w');
fprintf(fileID,'%8s\t%12s\r\n','Test','Maximum acceleration');
fprintf(fileID,'%8s\t%12.8f\r\n',A{:});
fclose(fileID);
Maybe also look into "table" variable type. With the second example
t = table(y_acc_max','VariableNames',{'Maximum acceleration'},'RowNames',tests)
t.Properties.DimensionNames{1} = 'Test'
writetable(t,'acc_max_t.txt','WriteRowNames',true,'Delimiter','\t')
Best Answer