Create and write t an .m file from MATLAB script
77 views (last 30 days)
Show older comments
Hi,
I would like to execute the following steps from a script:
- Create an .m file
- Write a string in the file
- 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!
0 Comments
Accepted Answer
Friedrich
on 6 Feb 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!