How to fix the errors Arise from moving .m files and functions in folders?
3 views (last 30 days)
Show older comments
I wrote some codes in Matlab for classification and I achieved the desired results, all the functions which were used in the program exist in Matlab projects path in C drive in my computer but for running another clustering program in Matlab, I removed the .m files and functions from the Matlab's projects path and saved all of them as a folder in C drive.
Now my first program shows errors about some of these functions while It didn't have any problems with the function's codes before, even I returned files to the previous state but this problem still exists.
Should I put the required functions in the Matlab's projects path or in another folder?
I'll be grateful to have your opinions about how to fix these errors.
16 Comments
Jan
on 19 Jun 2019
@phdcomputer Eng: I assume you have destroyed the Matlab installation. The clean solution is to uninstall Matlab, delete the installation folder, and to re-install it. Afterwards avoid working with admin privileges and do not write files in Matlab's installation folder.
Accepted Answer
Guillaume
on 18 Jun 2019
Ok so I should move all of the programs and functions (.m files ) in other drive except drive C (which Matlab has installed), for example drive D. Is It right?
No. You use any drive you want including the one where matlab is installed.
What you mustn't do, and this is not specific to matlab, is store any of your code/documents/pictures/etc. under Program Files or any of its subdirectory. Similarly, don't put your files in the Windows directory.
Typically, Matlab would have created a directory for you to store your files in. You can see what it is by typing
userpath
at the matlab prompt. It normally is C:\Users\yourusername\Documents\MATLAB.
As for, I think I can use absolute path for functions so the errors will be fixed
I don't know what Suyraansh Mata really meant, but it is not an option for functions. You cannot call a function by its full path. Even for scripts it's a bit iffy, the only way to invoke them by their full path is to use run.
The proper way to manage the scope of your functions is to put their folder on the matlab path. So you'd put all your files in
mypath = fullfile(userpath, 'SomeMeaningfulFolderName')
then
addpath(mypath)
to make them all visible to matlab.
More Answers (1)
Suryaansh Mata
on 17 Jun 2019
TLDR: Any folder/directory can be added to MATLAB's path using the 'addpath' command.
MATLAB can access functions in the following ways -:
1) Direct access via a function's name - This is possible when the function .m file exists in the current working directory or the defined matlab path
2) Absolute Path - Accessing a function not in the current path, using it's absolute path. If trying to call a function using it's absoute path matlab does not throw any error
3) Adding a function or a directory to MATLAB's path - If a function is in Matlab's search path, it can be accessed directly. Try seeing if the current folder is in MATLAB's path using the 'path' command and seeing from the list that is generated. Or a directory can be added to this path using the 'addpath' command.
This can help with any errors while trying to access functions not in the present directory.
2 Comments
Suryaansh Mata
on 18 Jun 2019
Just use the direct path like 'C:/Documents/MyFolder/myfunction.m' in case of Windows system or 'home/MyDirectory/myfunction.m' in case of Linux/Mac OS
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!