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

2 views (last 30 days)
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.
% 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 = {}'; % 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

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Jan 2022
@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.

More Answers (0)


Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!