1

プログラムの目的は、ファイルを調べて文字列を探し、文字列が含まれていた行でその文字列のすべてのインスタンスを吐き出すことです。

ファイルを検索して見つけることができましたが、それらをすべて格納できる配列または何かに入れることができませんでした。現時点では、最後のインスタンスが表示されます。最初のインスタンスを見つけるために、8 行目と 9 行目の間に簡単に 1 つの区切りを入れることができます。

問題の文字列を含むすべての行を保存する方法を誰かが知っていれば、それは大きな助けになります。

fid = fopen('....... file directory....')
prompt = 'What string are you searching for?  ';
str = input(prompt,'s');

i=0;
for j=1:10000;
tline = fgetl(fid);             %Returns next line of specified file
counter = counter + 1;          %Counts the next line
    if ischar(tline);           %Checks if the line is an array
    U=strfind(tline,str);       %Sets U to be 1 if strfind finds the string in line tline
        if isfinite(U) == 1                     
            what = tline;       %This is where I want to set the array positions equal to whatever tline is at that time, then move onto the next i and search for the next tline.
            i=i+1;
        end            
    end
end
4

2 に答える 2

1

次のことをお勧めします。

haystack = 'test.txt';

prompt = 'What string are you searching for?  ';
needle = input(prompt, 's');

% MS Windows
if ispc

    output = regexp(evalc(['!find /N "' needle '" ' haystack]), char(10), 'split');

    output = regexp(output, '^\[([0-9].*)\]', 'tokens');
    output = cellfun(@(x)str2double(x{:}), output(~cellfun('isempty', output)))';

% OSX/Linux   
elseif isunix

    output = regexp(evalc(['!grep -n "' needle '" ' haystack]), char(10), 'split');

    output = regexp(output, '^([0-9].*):', 'tokens');    
    output = cellfun(@(x)str2double(x{:}), output(~cellfun('isempty', output)))';

% Anything else: stay in MATLAB
else

    fid = fopen(haystack);
    output = textscan(fid, '%s', 'delimiter', '\n');
    fclose(fid);

    output = find(~cellfun('isempty', regexp(output{1}, needle)));

end

の内容test.txt:

garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage valuable garbage 
garbage garbage garbage 
garbage garbage valuable 
garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage valuable garbage 
garbage garbage garbage 
garbage garbage garbage 
garbage garbage garbage 

Windows または Linux でコードを実行するか、MATLAB バージョンを強制するとneedle = 'valuable'、正しい行番号が得られます。

output = 
    6   
    8  
   13

OS 固有のツールを使用する利点は、純粋な MATLAB バージョンよりもメモリ フットプリントがはるかに小さいことです (ファイル全体がメモリに読み込まれるわけではありません)。freadlこれを防ぐために (たとえばループを使用して) MATLAB でコードを拡張したとしても、OS 固有のツールは依然としてかなり高速です (さらにメモリ フレンドリーです)。それが私が最終手段としてそれを入れた理由です:)

于 2013-07-10T07:02:46.340 に答える
0

それらを構造体配列に格納できます。次に例を示します。

lines = struct([]); % to store lines and line numbers    

idx = 1;

fid = fopen('somefile.txt');

tline = fgets(fid);

while ischar(tline)

    U=strfind(tline, str);

    if numel(U) > 0                            

        lines(end + 1).line = tline; % save line
        lines(end).lineNo = idx;     % save its number 
        lines(end).U = U;            % where is str in the just saved line            

    end

    tline = fgets(fid);

    idx = idx + 1;

end

fclose(fid);

lineTxts    = {lines(:).line};   % get lines in a cell
lineNumbers = [lines(:).lineNo]; % get line numbers as matrix
于 2013-07-10T00:48:24.887 に答える