2

我是 MATLAB 脚本的新手。我有一个要删除的字符串,该字符串来自一个数组结构文件。要删除的字符串在每个循环中都不同。我可以将更改的字符串存储在变量中并使用变量和删除该字符串strrep吗?例如:

%% string i want to delete is "is_count_del=auto;"
delstrng=is_count_del_auto;
%%filetext is the name of the file from which is_count_del=auto; is to be deleted
r=strrep(filetext,'delstrng','');

我想我没有strrep正确使用。怎样才能达到预期的结果?

4

2 回答 2

1

strrep can be applied on cell arrays, so this makes your work really easy:

% # Read input file
C = textread('input.txt', '%s', 'delimiter', '\n');

% # Remove target string
C = strrep(C, 'is_count_del=auto', '');

% # Write output file
fid = fopen('output.txt', 'w');
for ii = 1:numel(C)
    fprintf(fid, '%s\r\n', C{ii});
end
fclose(fid)
于 2012-12-18T12:26:59.880 回答
1

如果我理解正确,您可以:

% open file to be filtered and output file
fin = fopen('file-to-be-filtered.txt');
fout = fopen('output-file.txt');

% for every line of a file-to-be-filtered ...
tline = fgets(fin);
while ischar(tline)
    % ... filter for all possible patterns        
    for delstring % -> delstring iterates over all patterns
        tline = strrep(tline, delstrng, '');
    end

    % save filtered line to file
    fprintf(fout, tline);

    % get next line
    tline = fgets(fin);
end
于 2012-12-17T19:43:07.090 回答