2013-05-02 2 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에 쓰고 싶습니다. 이 이름을 어떻게 알 수 있습니까?

나는 분명히 내가 원하지 않는 것을 H:\user4\matlab\myfile 제공 newfilepath=strcat(pathstr,'myfile').

를 원한다. 코드를 작성하려면 어떻게해야합니까?

답변

2

수동으로 부모 경로를 가져옵니다 :

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

또한 fullfile, filesepstrfind 사용합니다. Fullfile은 파일 및 경로로 작업하는 동안 문자열을 연결하는 데 정말 좋습니다.

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

또는 위의 디렉토리의 부모 디렉토리를 참조합니다 따라서 매트랩 이해 '..'를 사용하고

4

나는 두 번 다음 fullfilefileparts를 사용한다고 생각 :

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