필터 지우기
필터 지우기

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

조회 수: 16 (최근 30일)
Farhad Mirsaeidghazi
Farhad Mirsaeidghazi 2017년 10월 31일
댓글: Walter Roberson 2018년 9월 20일
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
Farhad Mirsaeidghazi
Farhad Mirsaeidghazi 2017년 10월 31일
what's the appropriate command for that?
Walter Roberson
Walter Roberson 2017년 12월 8일
xlswrite() or writetable() or fopen/fprintf/fclose

댓글을 달려면 로그인하십시오.


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
Georgios Kokogiannakis 2018년 9월 20일
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);

댓글을 달려면 로그인하십시오.

카테고리

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