filename1 = ['*new.txt'];
fid_t=fopen(filename1,'r')
This cannot work: '*new.txt' is not a valid file name, because the * is not allowed.
fid_p = fopen('final.txt','w');
if fid_p < 0, error('Cannot open file for writing.'); end
x = dir ('*new.txt');
for i = 1:length(x)
filename1 = x(i).name;
fid_t = fopen(filename1, 'r');
if fid_t < 0, error('Cannot open file for reading.'); end
data = fread(fid_t, inf, '*char');
fwrite(fid_p, data, 'char');
fclose(fid_t);
fprintf(fid_p,'\n');
end
fclose(fid_p);
This take the file name from the list of files in x. In addition it uses FREAD and FRWITE, because it is faster to access the files in binary mode.
Never use FOPEN without testing the success.
Working with relative paths is less reliable than absolute path names:
Folder = 'C:\Your\Folder';
FileList = dir (fullfile(Folder, '*new.txt'));
for iFile = 1:numel(FileList)
FileName = fullfile(Folder, FileList(iFile).name);
...
end
Best Answer