Append varying length vectors to csv file using dlmwrite
1 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149650/image.jpeg)
Producing this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149651/image.jpeg)
0 个评论
回答(1 个)
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).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!