Is it possible to update the text inside other scripts/functions using a separate script?

In development of a new tool, I will want to update the version and date in each script when a new version is ready to be used. Right now there are multiple scripts that will need the update, and to save time, I would like to create a script that calls to each individual function to change the date and version.
Let's say I have a function with a header:
function test = dummy()
%% Header
% Name : dummy
% Rev : 3.0.0
% Date : 2022/03/28
% Author : Steven Manz
% Desc :
%
% REVISION HISTORY
% 1.0.0 : Initial release
% 3.0.0 : Reformatted entire tool for production use
%
% INPUTS
% -------------------------------------------------------------------------
end
...
Is it possible to write a script that will search for Rev and change the revision as well as the history, then change the date to the current date?
Using another function to change the text inside a separate function is something I have never tried before and cannot seem to find any information for anywhere else. Is this possible in MATLAB?

4 Comments

If your files are simple .m files, there shouldn't be any problem. Those are text files you could edit with normal Matlab tools (just make sure they aren't being edited when you run your code). If you're working with mlapp or mlx files I don't know if there is a solution.
I see. For now they are just simple .m files. And I wouldn’t write a code to automate the process. I just want to run it as needed and it update certain areas. But how do I read and write from a .m file? I know how to do this when accessing like a .txt or a .xlsx file.
.m files are stored as plain text. Since a few releases they are UTF-8 encoded by default, but if you don't use special characters in your char arrays/strings that doesn't matter. So all tools that work on txt files will work on m files.
I see. So you are saying I can use like textscan or fgets on a .m file?

Sign in to comment.

 Accepted Answer

Ok so thank you to @Rik, this worked for me...
I simply used the following to read into the file:
try
fid = fopen('dummy.m', 'r');
file_read = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
catch me
fclose('all');
disp('Error while reading dummy.m')
rethrow(me);
end
And the following to write back into the file:
try
fid = fopen('dummy.m', 'w');
fprintf(fid, '%s\n', dummy_string);
fclose(fid);
catch me
fclose('all');
disp('Error while writing to dummy.m')
rethrow(me);
end
Of course in between there will be edits made to file_read that I desire. And only these lines will be affected.
But you can read and write to and from any .m file the same as you would a .txt file!

More Answers (1)

While you can store the revision history for a file in the file itself, if you're going to build a lot of tools for others to use I strongly recommend using a source control system or SCM. You can control certain types of SCMs from within MATLAB; the documentation section to which I linked discusses Git and SVN. One benefit an SCM gives you is that if you make a change and then need to undo that change, reverting it is usually pretty easy. If you've modified the main or sole copy of the file you need to try to remember what modifications you made to implement that change and that can be difficult especially if that change was large, was complicated, or it's been a while since you made it.

1 Comment

That’s awesome! I didn’t know about that. I will look into this option for the development portion of my code. The one in this question is something that only I will use and has nothing to do with development. So for the purposes of this question that would not be useful. However, I am excited to look into this for future development.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!