Create and write t an .m file from MATLAB script

75 ビュー (過去 30 日間)
Enrico
Enrico 2014 年 2 月 6 日
回答済み: Friedrich 2014 年 2 月 6 日
Hi,
I would like to execute the following steps from a script:
  1. Create an .m file
  2. Write a string in the file
  3. Save and Close file
This is as far as I have gotten:
a='test string';
edit file.m
FID=fopen('file.m');
fprintf(FID, '%s', a);
fclose(FID);
These lines do not return an error. The file is generated in the current directory and opens in the editor, however no string is written to file and the file does not close.
Any ideas?
Thank You very much!

採用された回答

Friedrich
Friedrich 2014 年 2 月 6 日
Hi,
take a look at the documentation for fopen, it states:
"fileID = fopen(filename) opensthe file, filename, for binary read access,..."
Since you want to write you need to pass down an additional argument. Depending on what you like to do I guess either 'a' or 'w' is what you are looking. If you call
FID = fopen('file.m','a')
You would open or create new file for writing. Append data to theend of the file.
When you call
FID = fopen('file.m','w')
you would ppen or create new file for writing. Discard existing contents, if any.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by