フィルターのクリア

Need help for Renaming Matlab Image png

3 ビュー (過去 30 日間)
LinusL
LinusL 2021 年 8 月 13 日
編集済み: LinusL 2021 年 8 月 13 日
Hi,
I have a folder consist of images which has random name but its placing on index (sorting) is correct.
I want to rename those folder to 1,2,3,4,5 and so on, instead of its random name.
Example: 323A23.png to 1.png
Any expert can offer me guidance how to rename file.
Thanks.

採用された回答

Dave B
Dave B 2021 年 8 月 13 日
編集済み: Dave B 2021 年 8 月 13 日
I often find myself doing this sort of thing with the system command, but it's probably better to do it with movefile.
Suppose your files are in C:\myfiles
filepath='C:\myfiles'
movefile(fullfile(filepath, '323A23.png'), fullfile(filepath, '1.png'));
Of course you probably want to loop over many files. A pro tip is, before you rename all of your files to something that you didn't intend, have a look at what the code will do with a disp:
filelist=dir(fullfile(filepath, '*.png'));
for i = 1:numel(filelist)
oldname = filelist(i).name;
newname = [num2str(i) '.png']; % can also do newname = string(i) + ".png";
fprintf('will rename %s to %s\n'); %check these before running with the next line uncommented
%movefile(fullfile(filepath, oldname), fullfile(filepath, newname));
end
The system version of this just amounts to passing in the same text that you'd put into the shell, e.g. in windows it's something like:
system(['rename "' fullfile(filepath, oldname) '" "' fullfile(filepath, newname) '"'])
but it's easy to mess up the quotes which you need for file names that contain spaces, so I think movefile is probably better, and it will also be multi-platform!
  1 件のコメント
LinusL
LinusL 2021 年 8 月 13 日
編集済み: LinusL 2021 年 8 月 13 日
Thanks alot

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 8 月 13 日
編集済み: KSSV 2021 年 8 月 13 日
thepath =' '; % give the path of the folder
imgNames =dir( fullfile(thepath, '*.png') );
for img = 1 : numel(imgNames )
newName = fullfile(thepath, [num2str(img),'.png'] );
movefile( fullfile(thepath, imgNames(img).name), newName );
end
  4 件のコメント
LinusL
LinusL 2021 年 8 月 13 日
Brace indexing is not supported for variables of this type.
Error in Untitled (line 8)
movefile( fullfile(thepath, imgNames{ img }), newName );
KSSV
KSSV 2021 年 8 月 13 日
Try now..edited the answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by