How can I create a blank .txt file? I use Matlab R2014a.
I want to check whether file of specified name exists and if it does not, I want to create one.
Asked
Active
Viewed 5,540 times
-2
-
2This is easily google-able.. why not use that tool (google) in a first place? – Kyslik Nov 11 '15 at 12:06
-
1Ok, but can you show what you have already tried? – Mikhail_Sam Nov 11 '15 at 12:07
2 Answers
2
I do not like answering questions that just "ask" and provide no "I tried this...".
Nevertheless, there are many ways to accomplish what you are asking. This is one of them:
if exist('text.txt', 'file') == 0
disp('File does not exist, creating file.')
f = fopen( 'text.txt', 'w' );
fclose(f);
else
disp('File exists.');
end

ZerkTheMighty
- 15
- 7

Kyslik
- 8,217
- 5
- 54
- 87
1
to check if file exists, simply use the exist
command:
exist( filename, 'file' )
To create an empty file, you can simply use fopen
and fclose
:
if ~exist(filename, 'file' )
fid = fopen(filename,'w');
fclose(fid);
end

Shai
- 111,146
- 38
- 238
- 371
-
-
1probably too late, but to overwrite you use fprintf(fid,'your text'). You must open with 'w' to overwrite. If you want to append open with 'a'. – Ferro Luca Jun 19 '23 at 08:31