Matlab:获取文本文件的一部分行数

时间:2014-11-09 08:29:48

标签: 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 =
        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

如何获取GLOBAL DOF SET NODES, LABELS =部分的行数?

在这种情况下,数字为6

我认为可能找到GLOBAL DOF SET NODES, LABELS =行(使用strfind?)和结束空行。但是如何?

1 个答案:

答案 0 :(得分:0)

确实你可以使用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检索。)