フィルターのクリア

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

18 ビュー (過去 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:

採用された回答

Walter Roberson
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
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.
Harr
Harr 2023 年 1 月 18 日
Dear Walter,
Thank you very much! It worked and solved the problem perfectly!
Best regards /Harr

サインインしてコメントする。

その他の回答 (1 件)

dpb
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
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.
Harr
Harr 2023 年 1 月 18 日
Dear dpb,
Thank you very much for your answer, i keep it for future R2020b :)
Bet regards/ H

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by