cannot find function even it is in path

53 次查看(过去 30 天)
Durga Lal Shrestha
Durga Lal Shrestha 2024-12-5,1:07
编辑: Stephen23 2024-12-5,5:56
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 个评论
Stephen23
Stephen23 2024-12-5,5:56
编辑:Stephen23 2024-12-5,5:56
"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?

请先登录,再进行评论。

回答(3 个)

Star Strider
Star Strider 2024-12-5,1:54
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 2024-12-5,2:14
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 个评论
Star Strider
Star Strider 2024-12-5,2:49
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-12-5,4:16
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?

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by