Edit multiple Matlab.m files with Matlab

I have more than 50 + m files that worked in the previous driver version, but are outdated for the newer driver version. As a result, I need to find and replace various variable or field names, and sometimes edit the input variables for all these files. For example, I would like to find the line

src.aaaa = 100; 

and replace it with:

 src.bbbb = 100; 

another example:

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

with:

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

I searched and found this discussion , which allows you to search in multiple files, but not to edit and not to edit anything. I can handle Matlab, so I'm looking for a way to do this in Matlab. Any ideas?

+4
source share
4 answers

You can use the Find Files dialog that you placed (Ctrl-Shift-F) to find each file you are looking for, and then Find and Replace (Ctrl + F) the specific lines that you want to change.

As an example, find the file with src.aaaa = 100; using Ctrl + Shift + F. Then Ctrl + F and add src.aaaa = 100; in the upper text box and src.bbbb = 100; in the lower text box.

From your post, it is not clear whether this will be feasible, since I do not know how many different lines you would like to change in these m files. How many are there? Are m files similar or are they all different?

If there are certain variables you are looking for, you can write a script to search for a loop through all m files using the dir function. Read the m file in a string variable using fscanf . Then replace the variable in the string with strrep . And finally, using fprintf to write to the new .m file with the corrected variables.

Talk to:

+3
source

A little out of the box - but I would use the sed command - it does exactly what you want and quickly on it, but you need to call it using system and build the command line. If you are in windows, you may need to install it through msys or cygwin .

+2
source

m file that implements Sekkou's suggestion:

 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 
+1
source

Ergodicity answered with great code, but:

  • Some comments are written in German, not in English.
  • There is a general lack of commentary, which makes understanding difficult.

I tried to fix these problems with the code below (I'm not sure if the MATLAB code is very well used using the language: lang-matlab, so paste it into MATLAB to make it easier to read):

 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 ie 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 ie 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 
+1
source

All Articles