MATLAB: Adding headers when using fprintf to write to text

fprintf

Hi,
I am writing a matrix to text using the following codes:
fid = fopen('Datav1Call_1993.txt','w+');
for idx = 1:size(DataPaper,1)
line = DataPaper(idx,~isnan(DataPaper(idx,:))); % creates the line of data without NaNs
fprintf(fid,[repmat('%d ',1,length(line)),'\n'],line);
end
fclose(fid);
how can i add headers to the columns?

Best Answer

For example, after fopen() and before the loop:
if fid == -1
errorMessage = sprintf('Error opening Datav1Call_1993.txt')
uiwait(errordlg(errorMessage));
return; % Bail out
end
% Now write header string.
fprintf(fid, 'header 1 header 2 header 3 whatever \n');