所以到目前为止我有这个
file = fileread(filenames.txt) ;
data = textscan(file, '#:%2d \t Area:%5d \t Xp:%4d Yp:%4d \t Length:%3d B#:%2d \t R:%2d L:%2d Ch:%2d \t End 1:Xp:%4d Yp:%4d \t End 2:Xp:%4d Yp:%4d\n') ;
data = [data{1}, num2cell([data{2:end}])] ;
xlswrite('filenames.xlsx', data) ;但是我的文本文件看起来是这样的(包含不同行的倍数文件,这个例子有9加上文件索引行)
File Index: 2095303
#: 1 Area: 939 Xp:-397 Yp: 586 Length: 99 B#: 2 R: 2 L: 0 Ch:-1 End 1:Xp:-450 Yp: 588 End 2:Xp:-352 Yp: 572
#: 2 Area: 1185 Xp:-287 Yp: 294 Length:145 B#: 2 R: 1 L: 1 Ch: 0 End 1:Xp:-317 Yp: 359 End 2:Xp:-235 Yp: 244
#: 3 Area: 592 Xp:-242 Yp: 486 Length: 77 B#: 3 R: 2 L: 1 Ch: 0 End 1:Xp:-278 Yp: 488 End 2:Xp:-202 Yp: 477
#: 4 Area: 818 Xp:-144 Yp: 480 Length: 60 B#: 2 R: 0 L: 2 Ch: 1 End 1:Xp:-181 Yp: 488 End 2:Xp:-135 Yp: 451
#: 5 Area: 377 Xp: -23 Yp:-443 Length: 37 B#: 1 R: 0 L: 1 Ch: 0 End 1:Xp: -42 Yp:-459 End 2:Xp: -12 Yp:-460
#: 6 Area: 923 Xp: 32 Yp:-234 Length: 67 B#: 1 R: 0 L: 0 Ch: 0 End 1:Xp: -3 Yp:-260 End 2:Xp: 60 Yp:-212
#: 7 Area: 812 Xp: 150 Yp:-148 Length: 54 B#: 1 R: 0 L: 1 Ch: 0 End 1:Xp: 136 Yp:-130 End 2:Xp: 169 Yp:-161
#: 8 Area: 5968 Xp: 428 Yp: 432 Length:402 B#: 3 R: 3 L: 0 Ch:-1 End 1:Xp: 224 Yp: 468 End 2:Xp: 622 Yp: 356
#: 9 Area: 617 Xp: 714 Yp: 13 Length: 63 B#: 1 R: 0 L: 1 Ch: 0 End 1:Xp: 687 Yp: 35 End 2:Xp: 702 Yp: -22我认为文件索引行把我搞砸了,但我也需要这些信息。
发布于 2014-04-23 22:16:48
第一行肯定有问题。您需要分别处理它,然后用
data = textscan(file(22:end),...)您可能需要玩22,它可能是21,或改变取决于第一行如何变化在您的不同的文件。注意,如文档所示,在字符串上使用textscan时,它总是从字符串的第一个元素开始,而不是上次在字符串上使用textscan时停止使用的地方。
此外,您可能需要担心格式字符串中的尾部“\n”。根据您的操作系统,您可能需要'\r‘而不是'\n’。
最后,您很可能会发现,在使用textscan之后,数据处于向量单元格数组中,需要进一步的操作才能将数据转换成适合使用xlswrite的格式。
https://stackoverflow.com/questions/23255337
复制相似问题