Combine strings and doubles and export to .txt

2 visualizaciones (últimos 30 días)
Tobias Bramminge
Tobias Bramminge el 8 de Oct. de 2019
Comentada: Tobias Bramminge el 8 de Oct. de 2019
I need to make a .txt file on the format as shown below.
_____________________________________________________________
Node coordinates and Elements
Coordinates
1.0000 50.0000 20.5000 20.0000
2.0000 50.0000 20.5000 -20.0000
3.0000 50.0000 20.5000 6.6667
4.0000 50.0000 20.5000 -6.6667
5.0000 -50.0000 20.5000 -20.0000
6.0000 37.5000 20.5000 -20.0000
7.0000 25.0000 20.5000 -20.0000
8.0000 12.5000 20.5000 -20.0000
9.0000 0 20.5000 -20.0000
10.0000 -12.5000 20.5000 -20.0000
End Coordinates
Elements
1 27 32 33
2 27 26 32
3 24 29 30
4 24 23 29
5 37 36 34
6 37 3 36
7 39 35 14
8 39 29 35
End Elements
_______________________________________________________________________________
I've made following strings and doubles to assemble as above.
Header = 'Node coordinates and Elements';
Nstart = 'Coordinates\n';
Nend = 'End Coordinates\n';
Space = ' \n';
Estart = 'Elements\n ';
Eend = 'End Elements';
A = [1,50,20.5000000000000,20;2,50,20.5000000000000,-20;3,50,20.5000000000000,6.66666666666670;4,50,20.5000000000000,-6.66666666666670;5,-50,20.5000000000000,-20;6,37.5000000000000,20.5000000000000,-20;7,25,20.5000000000000,-20;8,12.5000000000000,20.5000000000000,-20;9,0,20.5000000000000,-20;10,-12.5000000000000,20.5000000000000,-20]
B = [1,27,32,33;2,27,26,32;3,24,29,30;4,24,23,29;5,37,36,34;6,37,3,36;7,39,35,14;8,39,29,35]
I tried fprint or dlmwrite, but only either the string or the doubles work. I've also tried converting the doubles to string, but unsuccessfully.
  3 comentarios
Tobias Bramminge
Tobias Bramminge el 8 de Oct. de 2019
i just tried this:
%Tobias V. Bramminge s134132
clc, clear all, close all;
Header = 'Node coordinates and Elements';
Nstart = 'Coordinates\n';
Nend = 'End Coordinates\n';
Space = ' \n';
Estart = 'Elements\n ';
Eend = 'End Elements';
A = [1,50,20.5000000000000,20;2,50,20.5000000000000,-20;3,50,20.5000000000000,6.66666666666670;4,50,20.5000000000000,-6.66666666666670;5,-50,20.5000000000000,-20;6,37.5000000000000,20.5000000000000,-20;7,25,20.5000000000000,-20;8,12.5000000000000,20.5000000000000,-20;9,0,20.5000000000000,-20;10,-12.5000000000000,20.5000000000000,-20]
B = [1,27,32,33;2,27,26,32;3,24,29,30;4,24,23,29;5,37,36,34;6,37,3,36;7,39,35,14;8,39,29,35]
fid2 = fopen('format1.txt','a');
fprintf(fid2,Header);
fprintf(fid2,Nstart);
fprintf(fid2,'%f',A);
fprintf(fid2,Nend);
fprintf(fid2,Space);
fprintf(fid2,Estart);
fprintf(fid2,'%f',B);
fprintf(fid2,Eend);
fclose(fid2);
But all my rows in the doubles become one row.
Tobias Bramminge
Tobias Bramminge el 8 de Oct. de 2019
I found a solution using a for-loop for the doubles as follows:
______________________________________
Header = 'Node coordinates and Elements\n';
Nstart = 'Coordinates\n';
Nend = 'End Coordinates\n';
Space = ' \n';
Estart = 'Elements\n ';
Eend = 'End Elements';
A = [1,50,20.5000000000000,20;2,50,20.5000000000000,-20;3,50,20.5000000000000,6.66666666666670;4,50,20.5000000000000,-6.66666666666670;5,-50,20.5000000000000,-20;6,37.5000000000000,20.5000000000000,-20;7,25,20.5000000000000,-20;8,12.5000000000000,20.5000000000000,-20;9,0,20.5000000000000,-20;10,-12.5000000000000,20.5000000000000,-20]
B = [1,27,32,33;2,27,26,32;3,24,29,30;4,24,23,29;5,37,36,34;6,37,3,36;7,39,35,14;8,39,29,35]
fid2 = fopen('format52.txt','a');
fprintf(fid2,Header);
fprintf(fid2,Nstart);
for ii = 1:size(A,1)
fprintf(fid2,'%g\t',A(ii,:));
fprintf(fid2,'\n');
end
fprintf(fid2,Nend);
fprintf(fid2,Space);
fprintf(fid2,Estart);
for iii = 1:size(B,1)
fprintf(fid2,'%g\t',B(iii,:));
fprintf(fid2,'\n');
end
fprintf(fid2,Eend);
fclose(fid2);
________________________________________________________
I would appreciate a smarter solution though.
But thanks for comments so far!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 8 de Oct. de 2019
Editada: Stephen23 el 8 de Oct. de 2019
You can do this quite easily without any loops:
C1 = {'Node coordinates and Elements','Coordinates'};
C2 = {'End Coordinates','','Elements'};
C3 = {'End Elements'};
A = [1,50,20.5000000000000,20;2,50,20.5000000000000,-20;3,50,20.5000000000000,6.66666666666670;4,50,20.5000000000000,-6.66666666666670;5,-50,20.5000000000000,-20;6,37.5000000000000,20.5000000000000,-20;7,25,20.5000000000000,-20;8,12.5000000000000,20.5000000000000,-20;9,0,20.5000000000000,-20;10,-12.5000000000000,20.5000000000000,-20]
B = [1,27,32,33;2,27,26,32;3,24,29,30;4,24,23,29;5,37,36,34;6,37,3,36;7,39,35,14;8,39,29,35]
% Define matrix formats:
fmtA = [repmat(' %9.4f',1,size(A,2)),'\n'];
fmtB = [repmat(' %5d',1,size(B,2)),'\n'];
% Export data to file:
[fid,msg] = fopen('temp2.txt','wt');
assert(fid>=3,msg)
fprintf(fid,'%s\n',C1{:})
fprintf(fid,fmtA,A.')
fprintf(fid,'%s\n',C2{:})
fprintf(fid,fmtB,B.')
fprintf(fid,'%s\n',C3{:})
fclose(fid);
Giving (also attached as a file):
Node coordinates and Elements
Coordinates
1.0000 50.0000 20.5000 20.0000
2.0000 50.0000 20.5000 -20.0000
3.0000 50.0000 20.5000 6.6667
4.0000 50.0000 20.5000 -6.6667
5.0000 -50.0000 20.5000 -20.0000
6.0000 37.5000 20.5000 -20.0000
7.0000 25.0000 20.5000 -20.0000
8.0000 12.5000 20.5000 -20.0000
9.0000 0.0000 20.5000 -20.0000
10.0000 -12.5000 20.5000 -20.0000
End Coordinates
Elements
1 27 32 33
2 27 26 32
3 24 29 30
4 24 23 29
5 37 36 34
6 37 3 36
7 39 35 14
8 39 29 35
End Elements
Note that I followed the format of the example output text that you wrote in your answer, where you used multiple space characters as the delimiter. In your later comments you used horizontal tab characters as the delimiter, making your question and comments inconsistent.
I do not know of any sprintf format that prints four decimal places for all values except zero.
  1 comentario
Tobias Bramminge
Tobias Bramminge el 8 de Oct. de 2019
Thank you very much!
And also for the follow-up comment! I see and will apply in future questions!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by