How can I edit a '.m' file by another '.m' file ?

Can we edit a '.m' file from another '.m' file ? Like we can modify/update a model and save it by a M script.

 Accepted Answer

Yes. Open it with fopen(), get lines out into strings with fgetl(), modify them as needed. Open an output file with fopen(), write the modified lines out. Close both files with fclose(). Delete the input file with delete(). Rename the output file to the input filename with movefile().

7 Comments

It will be like creating a new file and deleting old one.
Anyway purpose is served what I want.
Thanks..
I have 2 files both have same comment out sections/lines, If I don't want want to modify the comment section/line and I want to skip those then how do I do that? "fwrite" doesn't let me skip any line.
I have a input file with lot of lines and output file is exactly same except output has variable data different from input file. I want to read variable data from excel file and edit input file with the varible data read from excel file. How to achieve this ?
I have no idea what "If I don't want want to modify the comment section/line and I want to skip those" means. Wouldn't you have 3 file, at least? The two files that have comments in them, plus at least one output file that you're using fwrite() with?
In a loop, you can skip lines starting with a % like this
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if startsWith(strtrim(textLine), '%')
continue; % Skip lines starting with a %
end
% Now do something with textLine. Then....
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
Thanks for your warm reply. I am sorry I did not phrase the question very clearly. Let me just repharse my question in little bit more detail with examples.
First, I have a m-File which looks something likes this : (below is just an example)
%%%% Test1 %%%%%
% Front 2.5
% Rear 1.5
%%%% Model Parameters %%%%%
X_File
Y_File
Z_File
%%% Current %%%%
CUR_L_F = 0.3;
CUR_L_R = 0.3;
CUR_H_F = 1.6;
CUR_H_R = 1.6;
%%% Velocity %%%%
v1=[2,3,4];
c1=[10,20,30];
Next, I have another m-File which looks like this : (Note that only parameter values are different)
%%%% Test2 %%%%%
% Front 2.65
% Rear 1.98
%%%% Model Parameters %%%%%
X_File
Y_File
Z_File
%%% Current %%%%
CUR_L_F = 0.34;
CUR_L_R = 0.33;
CUR_H_F = 1.64;
CUR_H_R = 1.63;
%%% Velocity %%%%
v1=[2,3,4];
c1=[10.1,20.5,35];
Also, I have an Excel file with parameters name and their values in front of it, which I change during tests. When I am satisfied with results of the test I update the m-File using Excel File values but since there are like millions of parameters it takes time to update the mFile. So, I thought I can write a "script file" to let MATLAB do it for me. I tried following : ( I haven't read excel file but I can do that so that should not be problem )
clear
clc
% Open the file.
fileID1 = fopen('test1.m', 'r');
fileID2 = fopen('test2.m', 'w');
for i=1:20
textLine = fgetl(fileID1);
if isempty(textLine)==0
if textLine(1)=='%'
continue; % Skip lines starting with a %
end
end
fwrite(fileID2,[textLine,double(sprintf('\n'))]);
end
fclose(fileID1);
fclose(fileID2);
This gives me the result : ( I wanted comment sections/lines to remain as they were, wanted only uncommented part to be same as "test.m "
X_File
Y_File
Z_File
CUR_L_F = 0.3;
CUR_L_R = 0.3;
CUR_H_F = 1.6;
CUR_H_R = 1.6;
v1=[2,3,4];
c1=[10,20,30];
Hope I have made myself a little more clearer, Let me know if you need more info. Thanks :)
Hi, I am looking for copying data and comments from one .m file to another .m file
For example, if you have another section, say,
%%% Acceleration %%%
abc = 31;
def = 43;
How to append this new data along with comments to other file ?
@BABANBHAIGARI SHAFIULLAH, open a file for reading and writing. Then read until you recognize the section, then write to the output file:
% Open the input file for reading in text mode.
inputFileID = fopen(inputFullFileName, 'rt');
% Open the output file for appending in text mode.
outputFileID = fopen(outputFullFileName, 'at');
% Read the first line of the file.
textLine = fgetl(inputFileID);
lineCounter = 1;
writeToOutput = false; % Set to true once we get to the section we want.
while ischar(textLine)
% Print out what line we're operating on to the command window.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(inputFileID);
lineCounter = lineCounter + 1;
if contains(textLine, '%%% Acceleration %%%')
writeToOutput = true;
end
if writeToOutput
fprintf(outputFileID, '%s\n', textLine);
end
end
% All done reading all lines, so close the files.
fclose(inputFileID);
fclose(outputFileID);
It worked for me. Thank you @Image Analyst for the detailed explanation.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!