MATLAB: How to programmatically create a script with text without saving it

appendeditMATLABwithout saving

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.

Best Answer

Str = sprintf('Hello!\n'Here I am.\n');
EditorService = com.mathworks.mlservices.MLEditorServices;
EditorService.newDocument(Str);
doc matlab.desktop.editor