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

조회 수: 9 (최근 30일)
Robert
Robert 2018년 8월 14일
편집: Jan 2021년 9월 28일
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
Stephen23
Stephen23 2018년 8월 14일
편집: Stephen23 2018년 8월 14일
"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 2018년 8월 14일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 8월 14일
편집: Jan 2021년 9월 28일
Str = sprintf('Hello!\nHere I am.\n');
EditorService = com.mathworks.mlservices.MLEditorServices;
EditorService.newDocument(Str);
doc matlab.desktop.editor
  댓글 수: 1
Robert
Robert 2018년 8월 14일
편집: Robert 2018년 8월 14일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by