user684793
user684793

Reputation: 111

How to make matlab see functions defined in .m files?

I'm a total newbie to MATLAB but I have to write some code in it. I've had problems with making MATLAB see functions I've defined in external .m files. This is what I've done: I've created a file named, say, foo.m in my home dir with the following contents:

function [y] = foo(x)
% description
y = x + 1

When I run matlab (my home dir is matlab's workdir) it does not see foo function - it replies with standard ??? Undefined function or variable 'foo' message. BUT help foo or which foo return correct data printing help text and pointing on foo.m file respectively.

I must be missing something but I have no idea what it is. This is getting very annoying.

Oh, after several trial and error attempts I've managed to call that function. Unfortunately I can't remember the sequence of steps I've performed. Moreover after restarting matlab it returns to its usual 'Undefined function or variable' response.

I have 7.11.0.584 matlab running on linux.

Upvotes: 1

Views: 8060

Answers (4)

Wajahat
Wajahat

Reputation: 463

Although coming late but I hope it will help someone. If in the folder where the function you are calling is residing, there is any other function with the same name as one of the functions from MATLAB toolboxes, then Matlab will not recognize its license and therefore will disable the whole folder from execution, no matter it is properly added to the path. The help will display though.

In order to check it, type:

which name_of_func.m

and you will get the path with "%Has no license available" message. If it is your own function, you should not get this message but only the path. Therefore, find the function in this folder which has the same name as a MATLAB toolbox functions, and rename it. I will solve the problem :).

Best Regards Wajahat

Upvotes: 0

crobar
crobar

Reputation: 2939

It seems you're having some trouble with addpath. Try opening the file in the matlab editor and adding a break point in the file. If the file is not on Matlab's path, matlab should ask if you want to change directory or add the file to the path, choose add to the path.

If this doesn't work, try changing the current working directory (displayed in the main window) to the same location as the m file and calling the function. If this doesn't work you're either getting the name wrong ar there's possibly something wrong with your installation.

Occasionally matlab has problems if it does not have write permission to the directory the file's in, so check that too, i.e. make sure admin rights aren't required for the directory or m file.

Oh, and try:

clear functions

to reload all functions into memory.

Upvotes: 1

user85109
user85109

Reputation:

MATLAB needs to be told which directories to search over to access those m-files. Clearly it cannot be left to search over your entire disk drives. The MATLAB search path is a list of directories that will be searched in specific order to find your functions.

help addpath
help pathtool

You should never put those files anywhere in the official MATLAB toolbox directories. Choose an entirely separate directory.

Finally, be careful not to name your own functions to match the names of existing MATLAB functions. Otherwise, your very next question here will be why your code does not work properly. This is a common cause of strange and confusing bugs.

Upvotes: 1

Matt
Matt

Reputation: 2379

The function needs to be in MATLAB's path. Use pathtool to tell MATLAB where to find your function. Note that if you name a function the same name as an existing function, MATLAB will use whichever function it finds first according to the order that the paths are listed as you see them in pathtool.

Upvotes: 0

Related Questions