필터 지우기
필터 지우기

Convert Python code to Matlab code

조회 수: 3 (최근 30일)
Pham Ha Tri
Pham Ha Tri 2021년 12월 9일
댓글: Pham Ha Tri 2021년 12월 9일
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
KSSV
KSSV 2021년 12월 9일
If you tell the purpose of your code, people who dont use python can also help you to code it in MATLAB.
Pham Ha Tri
Pham Ha Tri 2021년 12월 9일
Thanks. I just updated the question.

댓글을 달려면 로그인하십시오.

채택된 답변

Chunru
Chunru 2021년 12월 9일
% create some files for testing
writematrix([3 4], 'test1.txt');
writematrix([5 6], 'test2.txt');
dir
. .. test1.txt test2.txt
folder_path = '';
change_str = 'neg_';
rename_file(folder_path,change_str)
dir
. .. neg_test1.txt neg_test2.txt
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개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by