有沒有辦法簡單地加載目錄中的第一個文件的名稱,而不指定它的名稱,然後在每次迭代中移動到目錄中的下一個文件?如何在一個循環中從文件移動到文件,MatLab?
我有文件名用1,1.5,2,2.5,3,3.5結尾等命名的問題......所以在迭代中的num2str(X)將無助於定位文件。
我想重命名他們使用strrep(s1,s2,s3),但我又遇到了同樣的問題加載到一個循環!
我知道我應該先用更多計劃命名它們,但這些文件太大而無法再次運行模擬。
這是我必須重命名文件:
%%%RENAMING A FILE%%%
%Search directory to get number of files
d1=dir('\MATLAB\Data\NumberedQwQoRuns');
numfiles = length(d1)-2;
for a=1:numfiles
%Search subdirectory if necessary for count of those folders
d2=dir('\MATLAB\Data\NumberedQwQoRuns\Run'num2str(a));
subdir = length(d2)-2;
for b= 1:subdir
origname= PROBLEM???
Newname=['Zdata' num2str(b) '.txt']
Newfile= strrep(origname, origname, newname)
movefile(origname,Newfile)
end
end
非常感謝你的幫助, 阿比德一個
謝謝大家的幫助,有很多在這裏使用的新方法,但它非常大,所以我可以學習它們。然而,當我運行腳本時,我得到一個錯誤(???錯誤:文件:Altrenamefile.m行:13列: 7)我真的不明白什麼是錯的。表達式或語句不正確 - 可能是 不平衡({,或[。 – Abid
@Abid:我認爲它是調用:'[〜,ord] = sort(...)'。運算符](http://blogs.mathworks.com/loren/2009/09/11/matlab-release-2009b-best-new-feature-or/)是在MATLAB R2009b中引入的,所以如果你有舊版本,替換它與一個虛擬變量(這意味着我們不關心該值,並希望忽略它):'[dummy,ord] = sort(...)' – Amro
我認爲我很難理解或正確設置路徑。在文件NumberedQwQoRuns中,我有這些文件:Run1,Run2,... Run9。在這些文件中有我想重命名的.txt文件。謝謝你,Abid – Abid