Hello everybody,
I have a large non-homogeneous text file. And I need find the key words in the text then pick the value next to the key words. Here is one part of this text file:
sfhafjlhakjfhahfaoh(some text before) LAW NUMBER 8907 0 1 0 -1876.98 11440 1 8 2 2 2 7 8 LAW TYPE: 152 0 1 7 8 163 154 155 156 Geomaterial_2 - solid PHASE ELASTIC CONSTITUTIVE LAW FOR SOLID ELEMENTS AT CONSTANT TEMPERATURE USE OF EFFECTIVE STRESSES. ISOL = 1 NUMBER OF SUBINTERVALS.... NINTV= 1 YOUNG'S MODULUS .......... = 0.200000E+08 POISSON'S RATIO .......... = 0.300000 SPECIFIC MASS AS A MATERIAL LAW, RHO ...................... = 2670.00 LAW NUMBER 288 0 1 0 -13.45 110 1 8 2 2 2 9 8 LAW TYPE: 171 0 1 5 6 173 174 175 179 Geomaterial_2 - liquid PHASE WATER-AIR SEEPAGE- VAPOR -THERMAL COUPLED CONSTITUTIVE LAW FOR SOLID ELEMENTS ISOTROPIC CASE IANI = 0 FORMULATION INDEX FOR krw IKW = 0 FORMULATION INDEX FOR kra IKA = 0
I need the first values after the key words and the values in the third line after the key word 'LAW NUMBER' and 'LAW TYPE'. So in this case: two vector will be created: Lawnumber=[8907 288] and Lawtype=[152 288] and another two matrix of the third line will be [8 2 2 2 7 8; 8 2 2 2 9 8] for LAW NUMBER and [163 154 155 156; 173 174 175 179]
Mr.Oleg Komarov proposed me to use regexp. His code is very easy and powerful here is the lien: http://www.mathworks.com/matlabcentral/answers/13585-find-the-key-word-in-the-text-file-then-pick-the-value-next-to-it
here is the code:
% Import the whole file at once
fid = fopen('test.txt','r');text = textscan(fid,'%s','Delimiter','','endofline','');text = text{1}{1};fid = fclose(fid);% Parse with regexp
tk = regexp(text,'LAW NUMBER[\s\.=]+(\d+)|LAW TYPE[:\s]+(\d+)','tokens');% tk = regexp(text,'LAW TYPE\s+(\d+ ){2}(?:[^\n]+\n){2}(\d+ )+','tokens'); Optional code
% textscan([tk{1}{:}],'%f') Optional code
% COnvert to double
tk = reshape(str2double([tk{:}]),2,[])
It is very powerful to get the first value after key words. But the optional code doesn't work very well. Until now I am not successful to get the third line. Is someone could improve it and help me out ?
Thank you very much.
Gringoire
Best Answer