It's never a good idea to modify the matlab path (with addpath or others) just to read or write data files.
The simplest way to make sure that a file is read or written in the correct folder is to simply specify the folder as part of the name when you read/write. It's easy to do: use fullfile. You've done exactly that for your dir call. Why didn't you do the same for your fopen calls.
On a completely unrelated subject, matfiles is a fairly good variable name (dircontent may be better), FileName is also a good variable name, it's easy to understand what the variables contains. B is a horrendous variable name. It's also completely pointless in your code, it's unclear why you couldn't just have done
FileName = matfiles(i).name;
and not bother with the B or even better, keep using matfiles(i).name everywhere.
So, in the end:
testfiledir = 'E:\Data\Test\X';
matfiles = dir(fullfile(testfiledir, '*.txt'));
for i = 1:numel(matfiles)
Data = fileread(fullfile(testfiledir, matfiles(i).name));
Data = strrep(Data, ',', '.');
FID = fopen(fullfile(testfiledir, matfiles(i).name), 'w');
fwrite(FID, Data);
fclose(FID);
While you could also use readmatrix or readtable as suggested, it may change other formatting of the file and will be slower than the above due to the parsing it does.
Best Answer