-2

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
Thara
  • 489
  • 1
  • 8
  • 24

2 Answers2

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
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