Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to run a known M-file not in MATLAB path?

Subject: How to run a known M-file not in MATLAB path?

From: Michael Goffioul

Date: 5 Feb, 2004 09:13:02

Message: 1 of 13

Hi,

Is it possible to run a M-file for which the complete path is known, but
which
is not in the MATLAB path? In the same idea, if 2 functions with the same
name
are in the MATLAB path, how to call the shadowed one? (the second that is
reported by "which ... -full")

Thanks.
Michael.

Subject: How to run a known M-file not in MATLAB path?

From: Herbert Ramoser

Date: 5 Feb, 2004 09:15:41

Message: 2 of 13

Michael Goffioul wrote:
> Hi,
>
> Is it possible to run a M-file for which the complete path is known, but
> which
> is not in the MATLAB path? In the same idea, if 2 functions with the same
> name
> are in the MATLAB path, how to call the shadowed one? (the second that is
> reported by "which ... -full")

Take a look at function handles (@myfunc).

-Herbert

Subject: How to run a known M-file not in MATLAB path?

From: Michael Goffioul

Date: 5 Feb, 2004 09:36:10

Message: 3 of 13

> > Is it possible to run a M-file for which the complete path is known, but
> > which
> > is not in the MATLAB path? In the same idea, if 2 functions with the
same
> > name
> > are in the MATLAB path, how to call the shadowed one? (the second that
is
> > reported by "which ... -full")
>
> Take a look at function handles (@myfunc).

You cannot use full path with functions like "feval" or "str2func". And if
you create a function_handle with the function name only, using it results
in an error.

A few examples (Z:\Matlab\library is not in Matlab path):

str2func('Z:\Matlab\library\amplifier.m')
str2func('Z:\Matlab\library\amplifier')
feval('Z:\Matlab\library\amplifier.m')
feval(@amplifier)

Everything gives me an error because the function "amplifier" cannot be
found (case 4) or the function name is invalid (case 1-3)

Michael.

Subject: How to run a known M-file not in MATLAB path?

From: Herbert Ramoser

Date: 5 Feb, 2004 09:43:58

Message: 4 of 13

Michael Goffioul wrote:

>>>Is it possible to run a M-file for which the complete path is known, but
>>>which
>>>is not in the MATLAB path? In the same idea, if 2 functions with the
>
> same
>
>>>name
>>>are in the MATLAB path, how to call the shadowed one? (the second that
>
> is
>
>>>reported by "which ... -full")
>>
>>Take a look at function handles (@myfunc).
>
>
> You cannot use full path with functions like "feval" or "str2func". And if
> you create a function_handle with the function name only, using it results
> in an error.
>
> A few examples (Z:\Matlab\library is not in Matlab path):
>
> str2func('Z:\Matlab\library\amplifier.m')
> str2func('Z:\Matlab\library\amplifier')
> feval('Z:\Matlab\library\amplifier.m')
> feval(@amplifier)
>
> Everything gives me an error because the function "amplifier" cannot be
> found (case 4) or the function name is invalid (case 1-3)
>
> Michael.

But you can add Z:\Matlab\library to your path, create the fuction
handle and remove it from your path. I know this is not elegant but it
works ;-)

-Herbert

Subject: How to run a known M-file not in MATLAB path?

From: Michael Goffioul

Date: 5 Feb, 2004 10:23:21

Message: 5 of 13

> But you can add Z:\Matlab\library to your path, create the fuction
> handle and remove it from your path. I know this is not elegant but it
> works ;-)

I know, but this reveals pretty slow, especially since Z: is a network
drive,
and I would have to do it a lot in a loop.
I'd better than to simply add Z:\Matlab\library to the path once and work
with that. The remaining problem is that library\ contains user-prodided
methods whose name can be anything, like "amplifier".
The question is then how to avoid that this method is not shadowed by
another one with the same name ("amplifier" is a common name),
that is: how to call a method on the Matlab path, which is shadowed by
another one with the same name and placed higher in the precedence order.
I wonder whether this is something solvable in Matlab.

Thanks.
Michael.

Subject: How to run a known M-file not in MATLAB path?

From: Herbert Ramoser

Date: 5 Feb, 2004 11:23:38

Message: 6 of 13

Michael Goffioul wrote:
>>But you can add Z:\Matlab\library to your path, create the fuction
>>handle and remove it from your path. I know this is not elegant but it
>>works ;-)
>
>
> I know, but this reveals pretty slow, especially since Z: is a network
> drive,
> and I would have to do it a lot in a loop.
> I'd better than to simply add Z:\Matlab\library to the path once and work
> with that. The remaining problem is that library\ contains user-prodided
> methods whose name can be anything, like "amplifier".
> The question is then how to avoid that this method is not shadowed by
> another one with the same name ("amplifier" is a common name),
> that is: how to call a method on the Matlab path, which is shadowed by
> another one with the same name and placed higher in the precedence order.
> I wonder whether this is something solvable in Matlab.

Sorry, but I can not follow your argument. My suggestions was that you
create a function handle once at the start of your program, then you use
feval with this handle. In this case the function can not be shadowed
and I don't think that there will be any performance drop.

-Herbert

Subject: How to run a known M-file not in MATLAB path?

From: loren@mathworks.com (Loren Shure)

Date: 5 Feb, 2004 07:51:49

Message: 7 of 13

In article <1075973001.179571@dmz2.imec.be>, goffioul@imec.be says...
> > But you can add Z:\Matlab\library to your path, create the fuction
> > handle and remove it from your path. I know this is not elegant but it
> > works ;-)
>
> I know, but this reveals pretty slow, especially since Z: is a network
> drive,
> and I would have to do it a lot in a loop.
> I'd better than to simply add Z:\Matlab\library to the path once and work
> with that. The remaining problem is that library\ contains user-prodided
> methods whose name can be anything, like "amplifier".
> The question is then how to avoid that this method is not shadowed by
> another one with the same name ("amplifier" is a common name),
> that is: how to call a method on the Matlab path, which is shadowed by
> another one with the same name and placed higher in the precedence order.
> I wonder whether this is something solvable in Matlab.
>
> Thanks.
> Michael.
>
>
>
>

If you are worrying about shawdowing, you could cd to the directory, create
your function handle, and then cd back.

--Loren

Subject: How to run a known M-file not in MATLAB path?

From: Lothar Schmidt

Date: 5 Feb, 2004 15:13:28

Message: 8 of 13

"Michael Goffioul" <goffioul@imec.be> schrieb im Newsbeitrag news:1075968782.460520@dmz2.imec.be...
> Hi,
>
> Is it possible to run a M-file for which the complete path is
> known, but which is not in the MATLAB path?


[inside script or function]
% remember current directory
current_path=pwd;
% change directory
eval(['cd ' known_path_to_function])
% run function in actual directory
function_which_is_not_in_path(parameters)
% return to 'current' directory
eval(['cd ' current_path])

Subject: How to run a known M-file not in MATLAB path?

From: Michael Goffioul

Date: 5 Feb, 2004 15:32:50

Message: 9 of 13

> > I know, but this reveals pretty slow, especially since Z: is a network
> > drive,
> > and I would have to do it a lot in a loop.
> > I'd better than to simply add Z:\Matlab\library to the path once and
work
> > with that. The remaining problem is that library\ contains user-prodided
> > methods whose name can be anything, like "amplifier".
> > The question is then how to avoid that this method is not shadowed by
> > another one with the same name ("amplifier" is a common name),
> > that is: how to call a method on the Matlab path, which is shadowed by
> > another one with the same name and placed higher in the precedence
order.
> > I wonder whether this is something solvable in Matlab.
>
> Sorry, but I can not follow your argument. My suggestions was that you
> create a function handle once at the start of your program, then you use
> feval with this handle. In this case the function can not be shadowed
> and I don't think that there will be any performance drop.

Just for your information, the context is the following. I have a class
constructor
function that is given a string as argument like:

x = entity('LNA', 'amplifier');

Here, 'amplifier' is a model, and it actually corresponds to a M-file
located into
some known standard directory. To not mess the users workspace and path, I'd
like to avoid to add this standard model pool dir to Matlab path. However,
my
"entity" constructor needs to execute this function, only knowing its name
and
location. That was the start of my question.

About function handles, it is only valid if the function is in the path
(even
temporarily) when the handle is created. The solution I'm thinking about now
is
to cd into model dir, parse all M-files and create function handles. I can
then
construct a valid map<name,handle> once, and use it later when a specific
model is requested.

Thanks for all your answers.
Michael.

Subject: How to run a known M-file not in MATLAB path?

From: Titus Edelhofer

Date: 5 Feb, 2004 16:24:01

Message: 10 of 13

Hi Lothar,
just a comment: you can use the functional form of cd instead
of using eval here:

cd(known_path_to_function)
cd(current_path)

cheers,
Titus

"Lothar Schmidt" <vapooroop@gmx.net> wrote in message
news:bvtj1f$uan$1@online.de...
> "Michael Goffioul" <goffioul@imec.be> schrieb im Newsbeitrag
news:1075968782.460520@dmz2.imec.be...
> > Hi,
> >
> > Is it possible to run a M-file for which the complete path is
> > known, but which is not in the MATLAB path?
>
>
> [inside script or function]
> % remember current directory
> current_path=pwd;
> % change directory
> eval(['cd ' known_path_to_function])
> % run function in actual directory
> function_which_is_not_in_path(parameters)
> % return to 'current' directory
> eval(['cd ' current_path])
>
>
>

Subject: How to run a known M-file not in MATLAB path?

From: Lothar Schmidt

Date: 7 Feb, 2004 07:19:20

Message: 11 of 13


"Titus Edelhofer" <tedelhof@mathworks.com> schrieb im Newsbeitrag > Hi Lothar,
> just a comment: you can use the functional form of cd instead
> of using eval here:
>
> cd(known_path_to_function)
> cd(current_path)
>
> "Lothar Schmidt" <vapooroop@gmx.net> wrote in message
> > eval(['cd ' current_path])

yepp, I knew that something is strange when I wrote my posting... i just didn't think clear enough ;-))

Subject: How to run a known M-file not in MATLAB path?

From: Lars Gregersen

Date: 7 Feb, 2004 10:36:55

Message: 12 of 13

On Thu, 5 Feb 2004 09:13:02 +0100, "Michael Goffioul"
<goffioul@imec.be> wrote:

>Hi,
>
>Is it possible to run a M-file for which the complete path is known, but
>which
>is not in the MATLAB path? In the same idea, if 2 functions with the same
>name
>are in the MATLAB path, how to call the shadowed one? (the second that is
>reported by "which ... -full")

There is a function called run that can do this for you.

Try:
  doc run


  Lars

Lars Gregersen
COMSOL A/S
http://www.comsol.dk

Subject: How to run a known M-file not in MATLAB path?

From: Brett Shoelson

Date: 7 Feb, 2004 10:31:03

Message: 13 of 13

Lars Gregersen wrote:
>
>
> On Thu, 5 Feb 2004 09:13:02 +0100, "Michael Goffioul"
> <goffioul@imec.be> wrote:
>
>>Hi,
>>
>>Is it possible to run a M-file for which the complete path is
> known, but
>>which
>>is not in the MATLAB path? In the same idea, if 2 functions with
> the same
>>name
>>are in the MATLAB path, how to call the shadowed one? (the
second
> that is
>>reported by "which ... -full")
>
> There is a function called run that can do this for you.
>
> Try:
> doc run
>
>
> Lars
>
> Lars Gregersen
> COMSOL A/S
> <http://www.comsol.dk>
>

Also, check out externalfcn.m, on MATLAB Central.

externalfcn('[x,y] = c:\mydir\foo.m')

Brett

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us