How create .M file and function in that file with name same as file Name

I want to create a .M file and function in that file which should accept input arguments.
example:
File with name as myFuntion and should contain following code in that file
function Myfunction(~,msg)
end

3 Commenti

suppose in my code i want dynamically create '.M' file and in that created file , it should contain function which accepts inputs.
EX: create 'myFile.m'
In 'myFile.m' write a funtion as
function myFile(~,y)
statement;
end
I use a series of fprintf commands when I want to dynamically write a .m file (e.g. I have a function that creates a template for a unit test for a given function name). There's not much else to it though beyond getting all the formatting right ini those fprintf instructions.

Accedi per commentare.

 Risposta accettata

funname = 'myFile';
filename = [funname '.m'];
[fid, msg] = fopen(filename, 'w');
if fid < 0
error('Failed to open file "%s" because "%s"', filename, msg);
end
fprintf(fid, 'function %s(~, y)\n', funname);
fprintf(fid, 'statement;'\n');
fprintf(fid, 'end\n');
fclose(fid);
clear(funname) %remove any cached function with that name

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by