Cannot create .txt file in Matlab online
4 views (last 30 days)
Show older comments
I am trying to create a .txt to put some in some numbers to pull back out later.
My code is:
fileID = fopen('MyFile.txt', 'w');
When I run the code it creates a varable fileID that is shown in the workspace panel (on the lower left of the screen) as a double. But no file is created over in the files panel and when I try to read from that file it then gives an error that the file doesn't exist.
0 Comments
Answers (1)
Star Strider
on 24 Sep 2023
Edited: Star Strider
on 25 Sep 2023
I am not certain what the problem is. Using fopen only creates the file ID, as you discovewred.
To write the file, you need to do something like this —
fileID = fopen('MyFile.txt', 'w');
A = magic(5)
fprintf(fileID, '%2d %2d %2d %2d %2d\n', A);
fclose(fileID);
type('MyFile.txt')
That should work as well on MATLAB Online.
EDIT — (25 Sep 2023 at 15:18)
Exporting it to your computer requires that you go to MATLAB Drive, check the file name, and then click on ‘Download’. (Added in EDIT.)
.
4 Comments
Star Strider
on 27 Sep 2023
Did you try your fprintf calls with my code (using them instead of my fprintf call)?
Can you try them here? (Use the Code button ‘Insert a line of code’ to format it as code and then run the code by clicking on the green Run arrow in the top toolstrip.)
I have never had any problems with MATLAB Online (that I did not cause myself).
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!