我有大约50多个m文件在以前的驱动程序版本中工作但是对于较新的驱动程序版本已经过时了。因此,我需要查找和替换各种变量或字段名称,有时还会编辑所有这些文件的变量输入。 例如,我想找到
行src.aaaa = 100;
并将其替换为:
src.bbbb = 100;
另一个例子是替换:
vid = videoinput('xxxx' ,1, 'yyy')
使用:
vid = videoinput('kkkkkk' ,1, 'zzzz')
我搜索并找到this discussion,它允许搜索多个文件,但不能真正编辑或替换任何内容。我可以处理matlab所以我正在寻找一种在matlab中做到这一点的方法。有什么想法吗?
答案 0 :(得分:3)
您可以使用发布的“查找文件”对话框(Ctrl-Shift-F)查找要查找的每个文件,然后“查找并替换”(Ctrl + F)要更改的特定行。
例如,使用Ctrl + Shift + F找到src.aaaa = 100;
的文件。然后按Ctrl + F并将src.aaaa = 100;
添加到上方文本框,将src.bbbb = 100;
添加到下方文本框。
从你的帖子来看,目前还不清楚这是否可行,因为我不知道你想在这些m文件中改变多少行。那里有多少? m文件是相似的还是它们都不同?
如果您要搜索特定变量,则可以编写脚本以使用dir
函数循环搜索所有m文件。使用fscanf
将m文件读入字符串变量。然后使用strrep
替换字符串中的变量。最后使用fprintf
写入带有更正变量的新.m文件。
请参阅:
答案 1 :(得分:2)
答案 2 :(得分:1)
实施Sekkou建议的m-File:
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
答案 3 :(得分:1)
遍历性回复了一些很棒的代码,但是:
我试图用下面的代码解决这些问题(我似乎无法使用“language: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