2013-05-02 115 views
1

我想以編程方式爲我的文本文件設置路徑。例如,更改文件夾名稱,matlab

file = 'H:\user4\matlab\myfile.txt'; 
[pathstr, name, ext] = fileparts(file) 

pathstr = H:\user4\matlab 

name = myfile 

ext = .txt 

我想寫所有文件在H:\user4\myfile。我怎樣才能得到這個名字。

我想newfilepath=strcat(pathstr,'myfile').

顯然它給H:\user4\matlab\myfile我不想要什麼。我怎樣才能寫我的代碼。

回答

2

手動獲取父路徑:

islashes = strfind(pathstr,filesep()); 
newfilepath=fullfile(pathstr(1:islashes(end)),'..','myfile') 

也使用fullfilefilesepstrfindFullfile非常適合在處理文件和路徑時連接字符串。

或者使用'..'其中Matlab的將理解,因此將參考前面的目錄的父目錄:

newfilepath=fullfile(pathstr,'..','myfile') 
4

我認爲你應該使用fileparts兩次,然後fullfile

file = 'H:\user4\matlab\myfile.txt'; 
[pathstr, name, ext] = fileparts(file); 
pathstr = fileparts(pathstr); 
fullfile(pathstr, [name ext]) 
相關問題