Search keyword in file [fid, result, data, next_line] = vb_read_from_key_line(fid,key) [fid, result, data, next_line] = vb_read_from_key_line(fid,key,next_line) --- Input fid : file handle key : keyword at the beggining of the line next_line : if thos is given, file pointer is at the next line of this line string --- Output fid : file pointer is at the next line of the keyword line data : [num_key_line x num_in_one_line] if result is empty, key is not find if result > 0, result is position index of key in next_line next_line : next line to last keyword line --- example text file num= 1.0 2 num= 1.1 3 num= 1.2 4 > [fid, result, data] = vb_read_from_key_line(fid,'num='); > data = [1.0 2; 1.1 3; 1.2 4] Masa-aki Sato 2008-07-25 Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [fid, result, data, next_line] = ... 0002 vb_read_from_key_line(fid, key, next_line) 0003 % Search keyword in file 0004 % [fid, result, data, next_line] = vb_read_from_key_line(fid,key) 0005 % [fid, result, data, next_line] = vb_read_from_key_line(fid,key,next_line) 0006 % --- Input 0007 % fid : file handle 0008 % key : keyword at the beggining of the line 0009 % next_line : 0010 % if thos is given, file pointer is at the next line of this line string 0011 % --- Output 0012 % fid : file pointer is at the next line of the keyword line 0013 % data : [num_key_line x num_in_one_line] 0014 % if result is empty, key is not find 0015 % if result > 0, result is position index of key in next_line 0016 % next_line : next line to last keyword line 0017 % 0018 % --- example text file 0019 % num= 1.0 2 0020 % num= 1.1 3 0021 % num= 1.2 4 0022 % 0023 % > [fid, result, data] = vb_read_from_key_line(fid,'num='); 0024 % > data = [1.0 2; 1.1 3; 1.2 4] 0025 % 0026 % Masa-aki Sato 2008-07-25 0027 % 0028 % Copyright (C) 2011, ATR All Rights Reserved. 0029 % License : New BSD License(see VBMEG_LICENSE.txt) 0030 data = []; 0031 0032 if nargin < 3, next_line = fgetl(fid); end; 0033 0034 % read data 0035 while 1 0036 if ~ischar(next_line), break, end; 0037 0038 result = strfind(next_line,key); 0039 if isempty(result), break, end; 0040 0041 x = sscanf(next_line(result+length(key):end),'%f'); 0042 data = [data; x']; 0043 0044 next_line = fgetl(fid); 0045 end 0046 0047 return 0048 0049 % seek to key 0050 %while 1 0051 % next_line = fgetl(fid); 0052 % if ~ischar(next_line), result = []; break, end; 0053 % 0054 % result = strfind(next_line,key); 0055 % if ~isempty(result), break, end; 0056 %end 0057 % 0058 %if nargout < 3, return; end 0059 0060 0061 0062 % K = STRFIND(TEXT,PATTERN) は、文字列 TEXT の中で、文字 PATTERN が出現 0063 % する最初のインデックスを出力します。 0064 0065 %next_line = 'hpi 001 -0.068694 -0.043644 -0.042834'; 0066 %key = 'hpi'; 0067 % result = strfind(next_line,key); 0068 % x = sscanf(next_line(result+length(key):end),'%d %f %f %f') 0069 % x = sscanf(next_line(result+length(key):end),'%f') 0070 % 0071 %return