Append varying length vectors to csv file using dlmwrite

6 views (last 30 days)
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:

Answers (1)

dpb
dpb on 2 Oct 2015
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).

Products

Community Treasure Hunt

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

Start Hunting!