Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
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 comentarios
Rik
Rik el 28 de Mzo. de 2022
.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 el 28 de Mzo. de 2022
I see. So you are saying I can use like textscan or fgets on a .m file?

Iniciar sesión para comentar.

Respuesta aceptada

Steven Manz
Steven Manz el 28 de Mzo. de 2022
Editada: Steven Manz el 28 de Mzo. de 2022
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!

Más respuestas (1)

Steven Lord
Steven Lord el 28 de Mzo. de 2022
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 comentario
Steven Manz
Steven Manz el 28 de Mzo. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by