How to open new text file and how to write on the text file using matlab code?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
How to open new text file and how to write on the text file using matlab code?
PS: How to go to next line(like we use Enter to go to next line) on the text file using code....can anyone help
Accepted Answer
Image Analyst
on 25 Jul 2015
Try something like this
fid = fopen(filename, 'wt');
if fid ~= -1
fprintf('This is line 1. Backslash n makes a new line.\n');
fprintf('My result is %f\n, result);
fprintf('My string = %s\n', myString);
fclose(fid);
else
warningMessage = sprintf('Cannot open file %s', filename);
uiwait(warndlg(warningMessage));
end
7 Comments
peyush
on 25 Jul 2015
after using the below code,no text file named result.txt opens why?
fid = fopen('result.txt', 'wt');
if fid ~= -1
fprintf('This is line 1. Backslash n makes a new line.\n');
fprintf('My result is %f\n, result');
fprintf('My string = %s\n', 'hello');
fclose(fid);
else
warningMessage = sprintf('Cannot open file %s', filename);
uiwait(warndlg(warningMessage));
end
Because I forgot to put fid as the first argument of fprintf. Fix is:
fid = fopen('result.txt', 'wt');
if fid ~= -1
fprintf(fid, 'This is line 1. Backslash n makes a new line.\n');
result = 42;
fprintf(fid, 'My result is %f\n', result);
fprintf(fid, 'My string = %s\n', 'hello');
fclose(fid);
else
warningMessage = sprintf('Cannot open file %s', filename);
uiwait(warndlg(warningMessage));
end
thanks but, how to clear all the characters(everything) from the text file?
Try this and see if it works:
delete(filename);
fid = fopen('result.txt', 'wt');
if fid ~= -1
fclose(fid);
else
warningMessage = sprintf('Cannot open file %s', filename);
uiwait(warndlg(warningMessage));
end
Walter Roberson
on 26 Jul 2015
It should not be necessary to delete the file. Opening with 'w' should remove the previous content. (To update a file, open it with 'a' instead of 'w'; when you open with 'w' you might have to fseek() to the beginning of the file to read it.)
But if you want extra safety and security of saving the old version in the recycle bin instead of blowing it away forever, do this:
recycle on;
delete(filename);
peyush
on 26 Jul 2015
thanks
More Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)