필터 지우기
필터 지우기

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

조회 수: 3 (최근 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 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