How to change comma to dott for multiple text files and save the changes in the same text files?

15 views (last 30 days)
Dear,
I have multiple text files (3 examples are attached) and i like to oppen each and replace all comma ',' with dot '.' then save the changes in the same text file. Any suggestions?
So far i have the following code:
MyFiles=dir('*.txt'); % reading all the text files in working directory
N=length(MyFiles); % Number of my files (Here i have 3 attached to this question but the code should work for 100's)
for k=1:N %Here i need to rad each text file in the loop
data=MyFiles(k);
tic; %Perform the replacing of ´,´ with ´.´
data = strrep(data, ',', '.');
%% I tried also using this: data = str2double(data);
toc;
% Now need to save the changes in the text files
% ,I am not sure if this part works either
FID = fopen(data, 'w');
fwrite(FID, Data, 'char');
fclose(FID);
end
I get the following error:

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jan 2023
The below code will write the changed files into a directory, creating it if needed.
projectdir = '.';
outputdir = 'modified';
if ~isdir(outputdir); mkdir(outputdir); end
MyFiles = dir( fullfile(projectdir, '*.txt')); % reading all the text files in working directory
filenames = fullfile({MyFiles.folder}, {MyFiles.name});
N = length(filenames); % Number of my files (Here i have 3 attached to this question but the code should work for 100's)
for k=1:N %Here i need to rad each text file in the loop
thisfilename = filenames{K};
data = fileread(thisfilename);
%replace comma with dot
mask = data == ',';
data(mask) = '.';
% Now need to save the changes in the text files
[~, basename, ext] = fileparts(thisfilename);
newfilename = fullfile( outputdir, [basename ext]);
[FID, msg] = fopen(newfilename, 'w');
if FID < 0
error('failed to open file "%s" because "%s"', newfilename, msg);
end
fwrite(FID, Data);
fclose(FID);
end
  2 Comments
Walter Roberson
Walter Roberson on 17 Jan 2023
Note:
If you were to upgrade to R2019a or later then you could skip all of this, and instead read the data using readmatrix() with the DecimalSeparator option.

Sign in to comment.

More Answers (1)

dpb
dpb on 17 Jan 2023
Your variable MyFiles is a dir() struct containing the list of files; addressing it does NOT open the file, simply refers to the content of that struct.
Change the to something more like
d=dir('*.txt'); % dir struct containing list of all the text files in working directory
N=numel(d);
for k=1:N
S=readlines(d(k).name); % read as string array
S=strrep(S,',','.'); % do the string substitution
writematrix(S,d(k).name,'QuoteStrings',0) % rewrite the converted file
end
NOTA BENE!!!! The above overwrites the file without any prompt or other safety features -- BE SURE TO BACKUP ALL YOUR FILES FIRST WHILE DEBUGGING!!!!!
BTW, with recent releases of MATLAB, using readmatrix or similar, you can specify the 'DecimalSeparator' named parameter pair value and avoid having to convert the files themselves. With that instead, you could simply write
d=dir('*.txt');
for k=1:numel(d)
data=readmatrix(d(k).name,'DecimalSeparator',',','NumHeaderLines',1);
% do whatever with each data set in turn here
....
% before going on to the next ...
end
  2 Comments
Walter Roberson
Walter Roberson on 17 Jan 2023
Edited: Walter Roberson on 17 Jan 2023
The user cannot use readlines() because they are using R2018b which was earlier than readlines() which needs R2020b.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!