Different path than current folder
조회 수: 9 (최근 30일)
이전 댓글 표시
My current folder is C:\Users\donald\Documents\MATLAB\RD2\A2\RD
I want to save to C:\Users\donald\Documents\MATLAB\RD2\A2\PE
I use name = strcat('A2','\PE\pe','1','.mat'); then later save with a saving function that i know works.
I get the error "Unable to write file A2\PE\pe1.mat: No such file or directory."
I do not want to change the current folder or use full path names
Thanks!
댓글 수: 0
답변 (1개)
Image Analyst
2012년 8월 3일
편집: Image Analyst
2012년 8월 3일
This is so easy. You can do it in one line with strrep(). See full demo:
% This is what you're starting with.
currentFolder = 'C:\Users\donald\Documents\MATLAB\RD2\A2\RD'
% Append a trailing slash so we don't convert RD2 as well as RD.
% Simply change \RD\ to \PE\ using strrep().
% HERE IS THE ONE SINGLE LINE OF CODE YOU WANT:
desiredFolder = strrep(upper([currentFolder '\']), '\RD\', '\PE\')
% If the folder doesn't exist, create it.
if ~exist(desiredFolder, 'dir')
% mkdir(desiredFolder);
end
% Build the full file name with fullfile.
fullFileName = fullfile(desiredFolder, 'pe1.mat')
Alternatively, you can just get the parent folder of RD and then append the folder name you want and the file name:
lastSlashPosition = find(currentFolder == '\', 1, 'last')
parentFolder = currentFolder(1:lastSlashPosition-1)
fullFileName = sprintf('%s/PE/pe1.mat', parentFolder)
% Note: forward slashes work just fine in Windows.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!