Convert Python code to Matlab code
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pham Ha Tri
am 9 Dez. 2021
Kommentiert: Pham Ha Tri
am 9 Dez. 2021
I need to convert following python codes to Matlab. Can anyone help me to convert following codes:
-The code is to access a folder path and change the name of every file inside: oldname.txt -> neg_oldname.txt.
import os
def rename_file(folder_path,change_str):
for file in os.listdir(folder_path):
old_file_path= os.path.join(folder_path,file)
if os.path.isfile(old_file_path):
old_file_name=os.path.basename(old_file_path)
new_file_name= change_str+old_file_name
new_file_path=os.path.join(folder_path,new_file_name)
os.rename(old_file_path,new_file_path)
folder_path =""
change_str=“neg_”
rename_file(folder_path,change_str)
2 Kommentare
KSSV
am 9 Dez. 2021
If you tell the purpose of your code, people who dont use python can also help you to code it in MATLAB.
Akzeptierte Antwort
Chunru
am 9 Dez. 2021
% create some files for testing
writematrix([3 4], 'test1.txt');
writematrix([5 6], 'test2.txt');
dir
folder_path = '';
change_str = 'neg_';
rename_file(folder_path,change_str)
dir
function rename_file(folder_path,change_str)
fn = dir(fullfile(folder_path, '*.*'));
for i=1:numel(fn)
if ~ismember(fn(i).name, {'.', '..'})
movefile(fullfile(folder_path, fn(i).name), fullfile(folder_path, [change_str fn(i).name]));
end
end
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!