4

我有一些 50+ m 文件在以前的驱动程序版本中工作,但对于较新的驱动程序版本已经过时。因此,我需要查找和替换各种变量或字段名称,有时还要编辑所有这些文件的变量输入。例如,我想找到这条线

src.aaaa = 100;

并将其替换为:

src.bbbb = 100;

另一个例子是替换:

vid = videoinput('xxxx' ,1, 'yyy')

和:

vid = videoinput('kkkkkk' ,1, 'zzzz')

我搜索并找到了这个讨论,它允许搜索多个文件,但不能真正编辑或替换任何内容。我可以处理matlab,所以我正在寻找一种在matlab中做到这一点的方法。有任何想法吗?

4

4 回答 4

3

您可以使用您发布的“查找文件”对话框 (Ctrl-Shift-F) 查找您要查找的每个文件,然后“查找并替换”(Ctrl+F) 您要更改的特定行。

例如,src.aaaa = 100;使用 Ctrl+Shift+F 查找文件。然后 Ctrl+F 并添加src.aaaa = 100;到上面的文本框和src.bbbb = 100;下面的文本框。

从您的帖子中,尚不清楚这是否可行,因为我不知道您想在这些 m 文件中更改多少不同的行。那里有多少?m 文件是相似的还是都不同?

如果您正在搜索特定的变量,您可以编写一个脚本来循环搜索使用该dir函数的所有 m 文件。使用 . 将 m 文件读入字符串变量fscanf。然后使用 替换字符串中的变量strrep。最后使用fprintf更正的变量写入新的 .m 文件。

参考:

于 2013-02-12T21:13:45.957 回答
2

有点开箱即用 - 但我会使用sed命令 - 它完全符合您的要求并且速度很快,但是您需要调用它system并构建命令字符串。如果您在 Windows 上,您可能需要通过msyscygwin安装它。

于 2013-02-12T23:15:30.547 回答
1

实施 Sekkou 建议的 m 文件:

clear all;
clc;


%% Parameter

directory           = 'd:\xxx';
oldString           = 'the old text';
newString           = 'the new text';
regularExpression   = '[\w]+\.m';


%% Determine files to manipulate

cd(directory)

allFilesInDirectory = dir;


%% Manipulieren der verweneten Dateien

disp('Manipulated Files:');

for idx = 1 : length(allFilesInDirectory)

    if (~isempty ( regexp(allFilesInDirectory(idx).name, '[\w]+\.m','match') ))

        disp(allFilesInDirectory(idx).name);

        % Read and manipulate Text
        fileIdRead  = fopen(allFilesInDirectory(idx).name, 'r');
        fileText    = fscanf(fileIdRead,'%c');

        fileTextNew = strrep(fileText, oldString, newString);

        fclose(fileIdRead);


        % Write Text
        fileIdWrite = fopen(allFilesInDirectory(idx).name, 'w');

        fprintf(fileIdWrite, '%c', fileTextNew);

        fclose(fileIdWrite);
    end

end
于 2014-08-21T06:43:29.357 回答
1

Ergodicity 回复了一些很棒的代码,但是:

  • 有些评论是德语而不是英语
  • 普遍缺乏评论,这使得难以理解

我试图用下面的代码解决这些问题(我似乎无法使用“语言:lang-matlab”很好地以 MATLAB 格式显示代码,因此将其粘贴到 MATLAB 中以便于阅读):

close all;
clear all;
clc;
%% Parameters
% The directory in which to replace files. Currently this code does not modify files in
% sub-directories
directory           = 'C:\Users\Name\Wonderful code folder';
% The string that will be replaced
oldString           = sprintf('terrible mistake');
% The replacement string
newString           = sprintf('all fixed now');
% The file name condition - what type of files will be examined
% It must contain any of the English character set (letters, numbers or underscore
% character i.e. a-zA-Z_0-9) and ends with a ".m" MATLAB extension (use \.txt for text files)
regularExpression   = '[\w]+\.m';

%% Determine files to update, and update them as necessary
% Change the current directory to the user-specified one
cd(directory)
% Put the details of all files and folders in that current directory into a structure
allFilesInDirectory = dir;
% Initialise indexes for files that do and do not contain oldString
filesWithStringIndex = 1;
filesWithoutStringIndex = 1;
% For the number of files and folders in the directory
for idx = 1 : length(allFilesInDirectory)

    % If the file name contains any of the English character set (letters, numbers or
    % underscore character i.e. a-zA-Z_0-9) and ends with a ".m" filetype...
    if (~isempty ( regexp(allFilesInDirectory(idx).name, '[\w]+\.m','match') ))

        % Open the file for reading
        fileIdRead  = fopen(allFilesInDirectory(idx).name, 'r');

        % Extract the text
        fileText = fscanf(fileIdRead,'%c');

        % Close the file
        fclose(fileIdRead);

        % Search for occurrences of oldString
        occurrences = strfind(fileText,oldString);

        % If an occurrence is found...
        if ~isempty(occurrences)

            % Replace any occurrences of oldString with newString
            fileTextNew = strrep(fileText, oldString, newString);

            % Open the file for writing
            fileIdWrite = fopen(allFilesInDirectory(idx).name, 'w');

            % Write the modified text
            fprintf(fileIdWrite, '%c', fileTextNew);

            % Close the file
            fclose(fileIdWrite);

            % Update the list of files that contained oldString
            filesWithString{filesWithStringIndex} = allFilesInDirectory(idx).name;

            % Update the index for files that contained oldString
            filesWithStringIndex = filesWithStringIndex + 1;

        else
            % Update the list of files that did not contain oldString
            filesWithoutString{filesWithoutStringIndex} = allFilesInDirectory(idx).name;

            % Update the index for files that did not contain oldString
            filesWithoutStringIndex = filesWithoutStringIndex + 1;

        end
    end
end
%% Display what files were changed, and what were not
% If the variable filesWithString exists in the workspace
if exist('filesWithString','var')
    disp('Files that contained the target string that were updated:');
    % Display their names
    for i = 1:filesWithStringIndex-1, disp(filesWithString{i}); end
else
    disp('No files contained the target string');
end
% Insert a clear line between lists
disp(' ');
% If the variable fileWithoutString exists in the workspace
if exist('filesWithoutString','var')
    disp('Files that were not updated:');
    % Display their names
    for j = 1:filesWithoutStringIndex-1, disp(filesWithoutString{j}); end
else
    disp('All files contained the target string.');
end
于 2015-03-28T07:05:56.557 回答