필터 지우기
필터 지우기

persistence of path when using mfilename

조회 수: 1 (최근 30일)
Brian
Brian 2018년 8월 3일
댓글: Image Analyst 2018년 8월 4일
I have a script within a subfolder which is trying to reference the parent directory (two levels up), but am getting some unexpected behavior:
function varargout = myfun(varargin)
...
[myDir,~,~] = fileparts(mfilename('fullpath')); %this function directory
mainScriptDir = fullfile(myDir,'..','..'); %main script directory
...
%end myfun.m
end
the first time I run this I get something like
myDir = ...\parentDir\sub1\sub2 (myfun is located in sub2)
and the main script directory is '...\parentDir\..\..' which would be what I expect.
Subsequent calls yield:
myDir = ...\parentDir\sub1\sub2\..\..\sub1\sub2
myDir = ...\parentDir\sub1\sub2\..\..\sub1\sub2\..\..\sub1\sub2
etc.
Why does the path persist and continue to append with subsequent calls? Shouldn't the myfun workspace reset with each call? Is there a function I need to call to reset this behavior when using relative paths?
Thank you for your help!
Brian
  댓글 수: 3
Brian
Brian 2018년 8월 3일
No, nor does it seem to matter whether I call myfun from sub2,mainScriptDir, or some other folder.
Walter Roberson
Walter Roberson 2018년 8월 4일
Are you using mainScriptDir to construct a filename that you run() ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2018년 8월 3일
You aren't calling cd() in your program anywhere are you?
  댓글 수: 2
Brian
Brian 2018년 8월 3일
Not currently, but eventually yes.
Image Analyst
Image Analyst 2018년 8월 4일
I wouldn't. And you'll probably have to attach the script that demonstrates the problem, because it doesn't make sense.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by