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

2 次查看(过去 30 天)
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 个评论
Rik
Rik 2022-3-28
.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.

请先登录,再进行评论。

采纳的回答

Steven Manz
Steven Manz 2022-3-28
编辑:Steven Manz 2022-3-28
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!

更多回答(1 个)

Steven Lord
Steven Lord 2022-3-28
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 个评论
Steven Manz
Steven Manz 2022-3-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by