How to change the function name to match its file name in a code?

Hello,
I have a simple question, I think!
If we have a function named hello.m but its file name is called Hi then how to change the function
name to Hi.m (to make it match the file name) NON-manually?
Imagine you need to do this for 565676577 number of files in a for-loop.
Do you know a way? it is greatly appreciated.
Babak

8 comentarios

Jiri Hajek
Jiri Hajek el 5 de Dic. de 2022
Editada: Jiri Hajek el 5 de Dic. de 2022
Hi, since all m files are just text files, you can use file editing functions to analyse the header, identify function name and rewrite as required. It will require some coding and testing, but it's not very difficult either.
Pehaps you can help us by sharing how the 565676577 function files were created in the first place?
Jan
Jan el 5 de Dic. de 2022
Editada: Jan el 5 de Dic. de 2022
565'676'577 files?! Seriously? The movefile command needs about 0.01 seconds per file when writing on a SSD. Then renaming your files takes about 64 days of processing time. But you want to change the contents of the files and rewrite them. So expect the code to run several 100 days.
"hello.m" and "Hi.m" are no valid function names. Do you mean a file called "Hi.m", which contains the function "hello", which should be renamed to "Hi"?
This renaming of the function is not trivial. The identification of the function name has to consider some exceptions:
% A comment
%{
A block comment
function notTheFunctionName
%}
function thisIsTheName
Or:
classdef thisIsMyClass
...
end
Scripts must be excluded also.
Please explain the unclear parts of the question.
I have a folder 'My_folder' which contains 1000 files. The mfiles are AA1.m, AA2.m, ... AA1000.m . For instance AA1 is defined as
function z = aa1(x,y)
z = x+y;
end
this is the function name.
TASK: we want to have a 1000 files in the folder 'My_folder' all having the names AA1.m, AA2.m, ... AA1000.m and the corresponding function names should match them. So, the first mfile should be
function z = AA1(x,y)
z = x+y;
end
%************************************************************************
PLEASE DO NOT TELL ME ANYTHING OTHER THAN THE SOLUTION. Simply show me how you code this. If you do
not like to do this then simply skip this and I am fine with it.
"...PLEASE DO NOT TELL ME ANYTHING OTHER THAN THE SOLUTION. Simply show me how you code this. If you do not like to do this then simply skip this and I am fine with it."
Maybe you should consider to pay somebody to this task for you - then you will get what you want
It is not your responsibility to answer my question. You do me a favor. But, if you decide to answer then I expect the answer to match my question.
Jan
Jan el 5 de Dic. de 2022
Editada: Jan el 5 de Dic. de 2022
@Mohammad Shojaei Arani: Please consider the Community Guidelines. SHOUTING and disrespectful communication is not welcome here.
It is useful, if you answer questions for clarifications. I have mentioned the problems of an automatic renaming already and you did not react yet. You did not clarify the problem of the confused function name "hello.m" and file name "Hi". It wastes the time of the readers, if you mention 565'676'577 files, although you have 1000 files only.
It motivates readers to assist you, if you show, what you have tried so far.
Soory Jan. Indeed, I am grateful to your very kind helps.
I solved my problem using your hints. Thanks!
Babak

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Dic. de 2022

Comentada:

el 6 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by