How to run m-file from another m-file by input string
Mostrar comentarios más antiguos
I have tried this code below trying to immediately run an m-file that was just created in another m-file :
savename = input('name : ');
f=fopen(savename,'w');
%%%writes in the created m-file
fclose(f);
run(save_name);
The above code neither produce any error or runs the script. Any comments? thanks.
2 comentarios
Geoff Hayes
el 16 de Jul. de 2014
Editada: Geoff Hayes
el 16 de Jul. de 2014
I was able to do something similar but had to change your code slightly
% use 's' to indicate that the result is a string and not an
% expression that should not be evaluated
savename = input('name : ', 's');
% open the file
f=fopen(savename,'w');
% write a line to print out a statement
fprintf(f,'fprintf(''this is a test\\n'');');
% close the file
fclose(f);
% use savename rather than save_name
run(save name);
And this worked fine with the output being
this is a test
and the input being test.m.
What are you writing to your m file? If you run this script from the Command Window, does it work or does it only fail when you try to call it from the other m file (as above)?
Arief Anbiya
el 17 de Jul. de 2014
Respuesta aceptada
Más respuestas (1)
David Sanchez
el 16 de Jul. de 2014
I do that some times just by doing this:
another_script = input('script name: ','s'); % and type the name of your other script
run(another_script);
with no need of either fopen or fclose
1 comentario
Geoff Hayes
el 16 de Jul. de 2014
Arief's code is creating a script from within another, and so needs the fopen and fclose.
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!