Append varying length vectors to csv file using dlmwrite

조회 수: 4 (최근 30일)
John
John 2015년 10월 2일
답변: dpb 2015년 10월 2일
I am attempting to append data to columns of a csv file using dlmwrite. Is there a way to remove the row offset that the function automatically introduces? In the code below I would like to enter the ouput2 dataset into elements (D2:E2),(D11:E11) of the 'test.csv'. However, the function leaves these elements empty and appends starting at row 4. Is dlmwrite the proper function for what I am trying to achieve (see below)? Thank you for any suggestions.
header = {'col1', 'col2', 'col3', 'col4', 'col5'};
output1 = rand(3,3);
output2 = rand(6,2);
% open file
fid = fopen('test.csv', 'w') ;
% write column headers
fprintf(fid, '%s,', header{1,1:end-1}) ;
fprintf(fid, '%s\n', header{1,end}) ;
% write data
dlmwrite('test.csv', output1, '-append', 'roffset', 0, 'coffset', 0);
dlmwrite('test.csv', output2, '-append', 'roffset', -length(output1), 'coffset', 3); % attempting use negative index to shift up
% close file
fclose(fid) ;
Attempting this:
Producing this:

답변 (1개)

dpb
dpb 2015년 10월 2일
There really is no way to produce A) above by two sequential writes of the data in columns 1:3 and 4:5 separately--sequential files are, after all, well "sequential" and dlmwrite can't solve that quandary.
You would have to save the two datasets and merge the first N rows, write those, and then write the remaining (or any equivalent of several options regarding what was intended/allowed for the empty columns).

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by