how can I use dlmwrite for string and matrix in single line ?
19 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Jang Bahadur Singh
on 8 Dec 2017
Edited: Walter Roberson
on 8 Dec 2017
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 Comments
Georgios Kokogiannakis
on 20 Sep 2018
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
on 20 Sep 2018
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);
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!