How to change comma to dott for multiple text files and save the changes in the same text files?
조회 수: 8 (최근 30일)
이전 댓글 표시
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:
댓글 수: 0
채택된 답변
Walter Roberson
2023년 1월 17일
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
Walter Roberson
2023년 1월 17일
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.
추가 답변 (1개)
dpb
2023년 1월 17일
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
Walter Roberson
2023년 1월 17일
편집: Walter Roberson
2023년 1월 17일
The user cannot use readlines() because they are using R2018b which was earlier than readlines() which needs R2020b.
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!