How to save each time step of for loop with a different .txt name

2 ビュー (過去 30 日間)
Flavio Croce
Flavio Croce 2022 年 1 月 27 日
コメント済み: Flavio Croce 2022 年 1 月 27 日
Hi,
I have a lerge number of .dat files and I have to modify some cells that respect certains conditions. For this reason I have builded a for loop and it works great, but I want to save each output of the loop (the .dat file modified named now "data") as .txt file whit the original name. I try whit many function but until now I haven't found a solution.
Here is the code that I developped.
clear
% this part gives you the txt file name list in your folder
dirName ='O:\rete_idro_pluvio\Digitalizzazione\Limnimetri\work\Aggiornamento indici-Matlab\20210806_TIC_BED'; % folder path
files = dir( fullfile(dirName,'*.dat') ); % list of all *.dat files
files = {files.name}'; % file names
%% for loop
for i = 1:numel(files) % for loop for each file
% readtable
data = readtable(files{i});
[r c]= size (data);
for y= 1:r
%for x = files(1,1): files(130,1)
if data{y,3}==330
data{y,3}=500;
writetable(data,sprintf('%files.txt',i));
end
end
end
Thanks!!!

採用された回答

Geoff Hayes
Geoff Hayes 2022 年 1 月 27 日
@Flavio Croce - if you just want to change the extension, then you could do something like
[~,name,~] = fileparts(files{i});
writetable(data,[name '.txt']);
where we use fileparts to get the name of the file (ingoring path and extension) and then just append the txt extension to it.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by