Matlab:获取文本文件部分的行数
这是数据的一部分:Matlab:获取文本文件部分的行数,matlab,Matlab,这是数据的一部分: GLOBAL DOF SET = 1 3 4 5 6 33 35 36 37 38 65 67 68 69 70 97 99 100 101 102 129 131 132 133 134 161 163 164 165 166 GLOBAL DOF SET NODES, LABELS =
GLOBAL DOF SET =
1 3 4 5 6 33 35 36 37 38
65 67 68 69 70 97 99 100 101 102
129 131 132 133 134 161 163 164 165 166
GLOBAL DOF SET NODES, LABELS =
1 UX 1 UZ 2 UX 2 UZ 3 UX
3 UZ 4 UX 4 UZ 5 UX 5 UZ
6 UX 6 UZ 7 UX 7 UZ 8 UX
8 UZ 9 UX 9 UZ 10 UX 10 UZ
11 UX 11 UZ 12 UX 12 UZ 13 UX
13 UZ
MASS INFORMATION:
TOTAL MASS = 12197.
CENTROID (X,Y,Z) = 30.000 0.0000 8.5809
MOMENT OF INTERTIA ABOUT ORIGIN:
IXX = 0.10651E+07 IYY = 0.18383E+08 IZZ = 0.17318E+08
IXY = 0.0000 IYZ = 0.0000 IZX =-0.31397E+07
如何获取全局DOF集合节点、标签=
零件的行数
在这种情况下,数字为6
我认为定位
全局DOF集节点、LABELS=
行(使用strfind?)和结束空行可能是件好事。但是怎么做呢?实际上,您可以使用strcmp、strfind或regexp来比较字符串。关键是获取要比较的字符串。这是通过使用fgetl函数逐行读取文件来完成的
# Open your file and assign it's handel to fileID:
fileID = fopen('yourFile.txt','r');
# Let's use the strcmp method and define your strings you test with:
yourFirstLine = ' GLOBAL DOF SET NODES, LABELS =';
# Your second line, in this case and empty line:
yourSecondLine = '';
# Use the variable flag to signal that you found your first line:
flag = 0;
iLine = 0;
while ~feof(fileID)
currentLine = fgetl(fileID);
# Check if header line matches with the current line and store in iMatchOne:
if strcmp(currentLine,yourFirstLine)
iMatchOne = iLine;
flag = 1;
end
# If you already found your first line it's time to start testing for the second line.
if flag == 1
if strcmp(currentLine,yourSecondLine)
iMatchTwo = iLine;
# You could decide here to break, because more matches with '' will be found!
end
end
iLine = iLine + 1;
end
希望这有帮助
非必要的:
(您还可以在第一个检查中添加if标志==0。另一种方法是在找到第一行后中断while循环,并在新的while循环中继续,并对第二行执行测试。请注意,在关闭/重新打开或手动重置当前行位置之前,不会重置fgetl检索的行编号。)您可以使用MATLAB
regexp
。试试这个-regexp(您的_行,'\S(GLOBAL.DOF.SET.NODES.LABELS.**)代码>如果不匹配,则从1开始不断递增计数器。当你找到一个匹配项,即非空矩阵时,立即停止,然后得到行号。你的最后一段确实概述了正确的方法。你被困在哪里?打开文件?将内容获取为行的单元格数组?调用strfind
?@BenVoigt,我不知道如何编码最后一行是空行
,第一行是xxx
。iLine
来自哪里?你是否假设它是一个从零开始并在循环中递增的变量?干杯,马虎,修正!