How to programmatically create a script with text without saving it?

9 visualizaciones (últimos 30 días)
Robert
Robert el 14 de Ag. de 2018
Editada: Jan el 28 de Sept. de 2021
What I'm trying to do is have my function start a new m-file and add text to it, but without saving it.
fopen directly saves the file and edit does not let me append content without typing it in the script myself.
The solution could look something like:
contents = 'Hello world!';
edit('tmp.m', contents);
Where contents is to be printed in the file without saving it. I know that edit only takes filenames for inputs but this is just for illustration.
  2 comentarios
Stephen23
Stephen23 el 14 de Ag. de 2018
Editada: Stephen23 el 14 de Ag. de 2018
"fopen directly saves the file"
Not really: fopen opens a file for writing or reading, or both. It does not save any data to any files.
Robert
Robert el 14 de Ag. de 2018
What I meant was that when using fopen with a non-existing file name, the file is created and stored on disk, which wasn't what I wanted.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 14 de Ag. de 2018
Editada: Jan el 28 de Sept. de 2021
Str = sprintf('Hello!\nHere I am.\n');
EditorService = com.mathworks.mlservices.MLEditorServices;
EditorService.newDocument(Str);
doc matlab.desktop.editor
  1 comentario
Robert
Robert el 14 de Ag. de 2018
Editada: Robert el 14 de Ag. de 2018
Thanks Jan for the quick response. newDocument was not accepted as a method for EditorService, but this worked for me:
str = sprintf('Hello!\nHere I am.\n');
editorService = com.mathworks.mlservices.MLEditorServices;
editorApplication = editorService.getEditorApplication();
editorApplication.newEditor(str);
One small thing: Your string contains a quote that shouldn't be there. Thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by