cannot find function even it is in path

48 Ansichten (letzte 30 Tage)
Durga Lal Shrestha
Durga Lal Shrestha am 5 Dez. 2024
Bearbeitet: Stephen23 am 5 Dez. 2024
MATLAB is displaying the error message Unrecognized function or variable. I added the function file to one of the existing paths, but MATLAB still can't find it.
To resolve this, I need to reset the MATLAB path and add the path containing the function file again. Is this a common occurrence? I haven't encountered this before.
  1 Kommentar
Stephen23
Stephen23 am 5 Dez. 2024
Bearbeitet: Stephen23 am 5 Dez. 2024
"Is this a common occurrence?"
No.
Make sure that you are not saving the function in the installation directory structure of any application.
Is there anything special about that folder?

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Star Strider
Star Strider am 5 Dez. 2024
I am not certain what your problem is.
In my startup.m file, I have these assignments at the beginning:
pathmat = path;
My_Path = userpath;
My_Dir = My_Path;
My_Work_Dir = genpath(My_Dir);
addpath(My_Work_Dir, '-end');
I have not added any paths in the last few years, however when I have eartlier, this always added them to the existing path. I have never had any problems with this. You can check it later by running the path function.
You can then check with something like this:
p = path;
p = split(p, ';');
plast = p(end-10:end);
Hovering your mouse cursor over ‘p’ and ‘plast’ should bring up a tooltip that shows the contents of the respective variables, so you do not have to display the entire list in your Command Window.
.

Durga Lal Shrestha
Durga Lal Shrestha am 5 Dez. 2024
Thanks for the response. I also have a startup file to setup path and even update the functions with git. But the problem here is matlab cannot find the function even I have copyied function file to the exisint path. Let's describe the problem again:
  1. Assume I have function my_function.m which is not in matlab path, so Matlab cannot find function when running or debuging the code
  2. Now I copied my_function.m to the one of the existing matlab path.
  3. I run again the same code, the matlab still cannont find my_function.m
  4. Then I need to restore path and add path again.
  1 Kommentar
Star Strider
Star Strider am 5 Dez. 2024
Include the URL of this thread in your message to them so you do not have to repeat everything here.
Please post back here with their solution and your results using it.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 5 Dez. 2024
If you try to run a script that is not in the current folder or on the path, it will popup a message prompting you to change the current folder to the folder where that file lives. You should do that and then it should run.
If that m-file calls another file that is not in that folder or on the search path, then you will get the message youi got. Make sure the folder where that other file lives is on the path by setting it's path with the Set Path button on the home tab of the tool ribbon.
If that doesn't work, type path in the command window and tell us what it says. Then tell us what the current folder is when you start running your m-file. And tell us what the folder is of the function it can't find (it must not be on the path even though you think it is). Are you sure you clicked the Save button when you added the folder to the path?

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by