MATLAB Answers

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

87 views (last 30 days)
Can we edit a '.m' file from another '.m' file ? Like we can modify/update a model and save it by a M script.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 Feb 2017
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().

  4 Comments

Show 1 older comment
Shubham Gupta
Shubham Gupta on 12 Apr 2019
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 ?
Image Analyst
Image Analyst on 12 Apr 2019
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);
Shubham Gupta
Shubham Gupta on 12 Apr 2019
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 :)

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!