How to save each time step of for loop with a different .txt name
2 次查看(过去 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!!!
0 个评论
采纳的回答
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 Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!