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

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by