MATLAB Versions 1 through 13

조회 수: 7 (최근 30일)
Chris Conant
Chris Conant 2017년 2월 28일
편집: Walter Roberson 2017년 2월 28일
Back in the old days with DOS, PC-MATLAB, AT-MATLAB, 386 MATLAB and early versions for Windows (MATLAB 4, MATLAB 5, etc.) nobody could care less whether the .m extension is UPPER CASE or lower case. Versions after 2010 (or so) do. Anybody have a clue how to "rename" over 6500 m-files in one easy swoop?
C:\> rename *.M *.m doesn't seem to work to well in the Windows "DOS" prompt. :)
Case sensitive variables are great. Case sensitive m-files? Not so much. Picky, picky, picky.
Thanks.
  댓글 수: 3
Jan
Jan 2017년 2월 28일
What exactly does "doesn't seem to work well" mean? The command works well and I hesitate now to suggest it again, but I cannot guess, what's the problem.
Chris Conant
Chris Conant 2017년 2월 28일
편집: Walter Roberson 2017년 2월 28일
Jan - for some reason the DOS rename didn't seem to change the letter case. rename (star).M (star).m still puked on the script when trying to execute it. Doesn't matter. This worked: dir (star).(star) /s > mathworks.txt
Open that in notepad, and then copy to Excel. After some magic - a column of commands show up:
cd('F:\MathWorks\MATLAB')
movefile BODEEX.M bodeex.m_bad
movefile bodeex.m_bad bodeex.m
movefile EXMODEL3.M exmodel3.m_bad
movefile exmodel3.m_bad exmodel3.m
movefile F2VXFN.M f2vxfn.m_bad
movefile f2vxfn.m_bad f2vxfn.m
movefile OPENH.M openh.m_bad
movefile openh.m_bad openh.m
movefile REPLOT.M replot.m_bad
movefile replot.m_bad replot.m
movefile SAMPLE.M sample.m_bad
movefile sample.m_bad sample.m
Copy and paste those cells into an m-file. Amazingly enough - all 5736 files converted in a heartbeat.
Thanks for your response.

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

채택된 답변

Jan
Jan 2017년 2월 28일
편집: Jan 2017년 2월 28일
You can use one of the many recursive dir() versions from the FileExchange, perhaps FEX:15859-subdir--a-recursive-file-search:
FileList = subdir('C:\YourBasePath\*.M');
for iFile = 1:numel(FileList)
newName = [FileList{iFile}(1:end-2), '.m'];
movefile(FileList{iFile}, newName);
end
But there are smarter tools for doing this directly, e.g. BulkRename or AF5 Rename your files. (Note: Bother tools can be downloaded free of charge for personal use, but there are professional licenses also.)
[EDITED] Thanks Guillaume:
Since Matlab R2016b and an alternative using fileparts:
FileList = dir('C:\YourBasePath\**\*.M');
for iFile = 1:numel(FileList)
folder = FileList{iFile}.folder;
name_ext = FileList{iFile}.name;
oldFile = fullfile(folder, name_ext);
[dummy, name] = fileparts(name_ext);
newFile = fullfile(folder, [name, '.m']);
movefile(oldFile, newFile);
end
Hm. A leaner version:
FileList = dir('C:\YourBasePath\**\*.M');
FileName = fullfile({FileList.folder}, {FileList.name});
for iFile = 1:numel(FileName)
newName = [FileName{iFile}(1:end-2), '.m'];
movefile(FileName{iFile}, newName);
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 2월 28일
If you are using MS Windows with FAT* then I gather that it is not possible to turn off case *in*sensitivity at the file system level. If you are using MS Windows with NTFS then I gather that it is possible to turn off case *in*sensitivity at the file system level, but not often done.
When case *in*sensitivity is on at the filesystem level, then if you attempt to movefile() a file to a name that is the same but a different case combination, then movefile() will consider the source and destination to be the same and will leave them unchanged. Therefore on such systems, you need to movefile() to a name that is not a case insensitive match for any existing file, and the movefile() to the desired name.
You might want to use tempname() to pick a temporary file name that is not in use.
Remember to use a bunch of try/catch and success tests in case one of the movefiles fails
Chris Conant
Chris Conant 2017년 2월 28일
Thanks. Work arounds are always available. I got around it - see my response to Jan. Thank you all for your responses.

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by