Column Header for .csv file [HELP]
Mostra commenti meno recenti
Hi guys ,
How to add the Column Header to the first row of my table below ?

For example , column 1 = No , column 2 = IMC (%), column 3 = Status.
I use dlmwrite() to collect my data in txt . Then , use readtable() and writetable() export data to csv. Then , I try to put column header but failed.
My current code :
dlmwrite('Data.txt',[ No ' ' IMC ' ' Status],'-append','delimiter','');
T = readtable('Data.txt','readvariablenames',false);
writetable(T,'Results.csv');
Thanks! ^^
3 Commenti
per isakson
il 14 Mar 2018
I deleted my answer to put your question on top of the list with no answers.
per isakson
il 15 Mar 2018
I failed to reproduce your results on R2016a.
zhixuan hong
il 15 Mar 2018
Risposte (1)
Image Analyst
il 15 Mar 2018
Try this, where I first write out the numerical data with csvwrite(), then I open the file and insert the column headers:
data = rand(5,3); % Create sample data
% Create filename.
fullFileName = fullfile(pwd, 'delete_me.csv'); % Whatever you want...
% First write out numerical data alone.
csvwrite(fullFileName, data)
% Now read back in entire file and save it.
txt = fileread(fullFileName)
% Open the file for output.
fid = fopen(fullFileName, 'wt');
% FIrst, write the column headers
fprintf(fid, 'No, IMC (%%), Status\n');
% Next, write the text we just read in, which is all the numerical data.
fprintf(fid, '%s', txt);
fclose(fid); % Close the file.
type(fullFileName); % Type to command window to see if it worked.
5 Commenti
zhixuan hong
il 15 Mar 2018
zhixuan hong
il 16 Mar 2018
Image Analyst
il 16 Mar 2018
Notice that I did 'wt', not 'wt ' like you did. Why are you adding spaces after the strings I gave you?
zhixuan hong
il 19 Mar 2018
Modificato: zhixuan hong
il 19 Mar 2018
Image Analyst
il 19 Mar 2018
To read it back in you'll have to specify a row input argument in csvread() to skip the header line. You could also use the importdata() function.
Categorie
Scopri di più su Data Import and Export in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

