Column Header for .csv file [HELP]

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

per isakson
per isakson 2018년 3월 14일
I deleted my answer to put your question on top of the list with no answers.
per isakson
per isakson 2018년 3월 15일
I failed to reproduce your results on R2016a.
zhixuan hong
zhixuan hong 2018년 3월 15일
so that is version problem right ( ?_?)

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2018년 3월 15일

0 개 추천

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

zhixuan hong
zhixuan hong 2018년 3월 15일
thanks for ur reply ... i will try ur ans later ^^ hopefully no version problem again. Im using r2015a.
Hi, i tried ur answer. still cant work ...
Error:
Error using fopen The file mode for fopen can not contain modes other than r, w, a, +, b, A, W or t.
Error in updateduitable>Start_analysis_Pushbutton_Callback (line 244) fid = fopen('Results.csv', 'wt ');
Code:
dlmwrite('Data.txt',[ No ' ' IMC ' ' Status],'-append','delimiter','');
T = readtable('Data.txt','readvariablenames',false);
writetable(T,'Results.csv');
txt = fileread('Results.csv' )
fid = fopen('Results.csv', 'wt ');
fprintf(fid, 'No, IMC (%%), Status\n ');
fprintf(fid, '%s', txt );
fclose(fid);
Image Analyst
Image Analyst 2018년 3월 16일
Notice that I did 'wt', not 'wt ' like you did. Why are you adding spaces after the strings I gave you?
zhixuan hong
zhixuan hong 2018년 3월 19일
편집: zhixuan hong 2018년 3월 19일
sorry , it is my fault ... the header is ok already , but something wrong for the content (data)...
Image Analyst
Image Analyst 2018년 3월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

질문:

2018년 3월 13일

댓글:

2018년 3월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by