2013-05-02 112 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]) 
相关问题