第3个回答 2020-04-17
文件放在哪里,直接可以运行。不用change
folder,会把m文件位置变成当前工作目录。
%save
m-file
as
aa.m
function
res=aa()
clc
p1
=
mfilename('fullpath')
i=findstr(p1,'\')
p1=p1(1:i(end))
cd(p1)
end
mfilename
file
name
of
currently
running
function
syntax
mfilename
p
=
mfilename('fullpath')
c
=
mfilename('class')
description
mfilename
returns
a
string
containing
the
file
name
of
the
most
recently
invoked
function.
when
called
from
within
the
file,
it
returns
the
name
of
that
file.
this
allows
a
function
to
determine
its
name,
even
if
the
file
name
has
been
changed.
p
=
mfilename('fullpath')
returns
the
full
path
and
name
of
the
file
in
which
the
call
occurs,
not
including
the
filename
extension.
c
=
mfilename('class')
in
a
method,
returns
the
class
of
the
method,
not
including
the
leading
@
sign.
if
called
from
a
nonmethod,
it
yields
the
empty
string.
tips
if
mfilename
is
called
with
any
argument
other
than
the
above
two,
it
behaves
as
if
it
were
called
with
no
argument.
when
called
from
the
command
line,
mfilename
returns
an
empty
string.
to
get
the
names
of
the
callers
of
a
matlab
function
file,
use
dbstack
with
an
output
argument.