Csvwrite a matrix with header

조회 수: 23 (최근 30일)
JFz
JFz 2015년 12월 11일
편집: Bhavin Gajjar 2018년 1월 6일
Hi,
I have a huge matrix (3000x3000) and I can write to a csv file with csvwrite('filename', cMatrix); But I also have a header defined in cellarray of strings as 'cHeader'. How should I save the matrix with header? In addition, I would also like to transpose the cHeader array to be the first column of the matrix. Is that possible?
Thanks,
Jennifer
  댓글 수: 1
jgg
jgg 2015년 12월 11일
편집: jgg 2015년 12월 11일
Is it absolutely necessary that you export directly to a .csv? The issue is that it's actually a lot easier to write data to an .xls file instead when it is mixed. You can then use the .xls file to save it as a .csv instead. ( http://www.mathworks.com/help/matlab/ref/xlswrite.html )
The way to do this for a .csv is cumbersome, because you have to use low level export functions directly. See here: http://www.mathworks.com/help/matlab/import_export/write-to-delimited-data-files.html#br2ypq2-1 for a tutorial.
Note in either case you want to cast your data to a cell, which should not pose a problem (just use mat2cell); it will also let you add your column vector of labels at that point.

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

답변 (2개)

Joseph Cheng
Joseph Cheng 2015년 12월 11일
simple way is to write the header in using fprintf
cHeader = {'ab' 'bcd' 'cdef' 'dav'}; %dummy header
commaHeader = [cHeader;repmat({','},1,numel(cHeader))]; %insert commaas
commaHeader = commaHeader(:)';
textHeader = cell2mat(commaHeader); %cHeader in text with commas
%write header to file
fid = fopen('yourfile.csv','w');
fprintf(fid,'%s\n',textHeader)
fclose(fid)
%write data to end of file
dlmwrite('yourfile.csv',yourdata,'-append');
  댓글 수: 3
Ugur Keskin
Ugur Keskin 2017년 3월 27일
One comment on this solution: the textHeader is also including a comma at the end of the textHeader. This is not desired since the row of a CSV should not end with a comma.
I think the easiest fix is to add the following:
TextHeader = TextHeader(1:end-1);
This will remove the last comma.
Guillaume
Guillaume 2017년 3월 27일
Or since R2013a, get rid of most of the lines building the header and simply:
textHeader = strjoin(cHeader, ',');
which will not put an extra comma

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


Bhavin Gajjar
Bhavin Gajjar 2018년 1월 6일
편집: Bhavin Gajjar 2018년 1월 6일
% Below code use the same logic described in above solution and it works fine. % This code shows the random data matrix of size 10x10 has appended in MyData.csv file with % heading of feature number and label
d = rand(10)
for i = 1:size(d,2) if i == 1 header1{i} = ['Label' num2str(i-1)] else
header1{i} = ['feature_' num2str(i-1)]; end end
header = strjoin(header1, ',');
fid = fopen('MyData.csv','w'); fprintf(fid,'%s\n',header) fclose(fid)
dlmwrite('MyData.csv',d,'-append');

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by