Combine strings and doubles and export to .txt
1 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Stephen23
2019-10-8
编辑:Stephen23
2019-10-8
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!