MATLAB: How to save a text file with headers

MATLABtext file

For context, the full code reads data from a text file, calibrates and converts the data to degrees, and then saves the new data in 4 columns with 108766 rows each. This is what I am currently using to save the new text file:
function calibratedData = applyCal(fname,Ey,components,rawData,Calib)
% applies calibration (gain and shift) to raw data; function returns
% calibrated data ONLY for 'components'!
calibratedData = [];
for i = find(components)
calibratedData(:,Ey(i).datacolumn) = ( rawData(:,Ey(i).datacolumn) - Calib(1,Ey(i).datacolumn) ) * Calib(2,Ey(i).datacolumn);
end
%save as textfile:
s = strtok(fname,'.');
SaveName = [s, datestr(now, ' yyyymmddTHHMMSS'),' calibrated.txt'];
save (SaveName, 'calibratedData', '-ASCII');
end
This works – but I need there to be a header at the top of each column. The headers need to be "LE Horizontal" "LE Vertical" "RE Horizontal" and "RE Vertical". Any help would be appreciated! Thank you!

Best Answer

function calibratedData = applyCal(fname,Ey,components,rawData,Calib)
% applies calibration (gain and shift) to raw data; function returns
% calibrated data ONLY for 'components'!
calibratedData = [];
for i = find(components)
calibratedData(:,Ey(i).datacolumn) = ( rawData(:,Ey(i).datacolumn) - Calib(1,Ey(i).datacolumn) ) * Calib(2,Ey(i).datacolumn);
end
%save as textfile:
s = strtok(fname,'.');
SaveName = [s, datestr(now, ' yyyymmddTHHMMSS'),' calibrated.txt'];
names={'"LE Horizontal"','"LE Vertical"','"RE Horizontal"','"RE Vertical"'};
writecell(names,SaveName,'delimiter','space');
save(SaveName, 'calibratedData', '-ASCII' '-append');
end
saves having to create the table just for the purpose of writing a header line. Second time come up in last couple of days...see