how can I use dlmwrite for string and matrix in single line ?

16 次查看(过去 30 天)
I want to write a txt file and when I use dlmwrite to write string and matrix together , output file include string in line1 and matrix in line 2 , how can I write them in single line ?
values='values.txt' %output file name
str='set ' %string
col=[1.2,2.2,3.2,4.2,5.2] %matrix
dlmwrite(values,str,'delimiter', '')
dlmwrite(values, col, '-append','delimiter', ' ')
output file is :
set
1.2 2.2 3.2 4.2 5.2
while I want to write :
set 1.2 2.2 3.2 4.2 5.2
Thanks , Farhad

回答(2 个)

Walter Roberson
Walter Roberson 2017-10-31
  4 个评论

请先登录,再进行评论。


Jang Bahadur Singh
Jang Bahadur Singh 2017-12-8
编辑:Walter Roberson 2017-12-8
str={'set '};
col=[1.2,2.2,3.2,4.2,5.2];
A1 =[a1 col];
er1=table(A1);
writetable(er1,'xx1.txt','Delimiter','\t','WriteRowName',true)
  2 个评论
Georgios Kokogiannakis
Hi,
I have the same question as above. writetable is extremely slow for a relatively big dataset.My task is to replace my current code which uses writetable with dlmwrite but I struggle as above (it will save me about 6 hours every time I run the code). I use fprintf for my headers of my csv file fprintf does not put the comma delimiter. Help please.
Walter Roberson
Walter Roberson 2018-9-20
data = round( 10 * rand(100,3), 1 ); %for example
headers = {'name1', 'another name', 'var3'}; %for example
ncol = size(data, 2);
fid = fopen('xx1.txt', 'wt');
fprintf(fid, '%s,', headers{1:end-1});
fprintf(fid, '%s\n', headers{end});
fmt = [repmat('%f,', 1, ncol-1), '%f\n'];
fprintf(fid, fmt, data.' ); %transpose is important
fclose(fid);

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by