Borrar filtros
Borrar filtros

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

16 visualizaciones (últimos 30 días)
Farhad Mirsaeidghazi
Farhad Mirsaeidghazi el 31 de Oct. de 2017
Comentada: Walter Roberson el 20 de Sept. de 2018
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

Respuestas (2)

Walter Roberson
Walter Roberson el 31 de Oct. de 2017
  4 comentarios
Farhad Mirsaeidghazi
Farhad Mirsaeidghazi el 31 de Oct. de 2017
what's the appropriate command for that?
Walter Roberson
Walter Roberson el 8 de Dic. de 2017
xlswrite() or writetable() or fopen/fprintf/fclose

Iniciar sesión para comentar.


Jang Bahadur Singh
Jang Bahadur Singh el 8 de Dic. de 2017
Editada: Walter Roberson el 8 de Dic. de 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 comentarios
Georgios Kokogiannakis
Georgios Kokogiannakis el 20 de Sept. de 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
Walter Roberson el 20 de Sept. de 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);

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by