How do I access a .m file in a subfolder?

13 views (last 30 days)
Hi Matlab community,
how do I access a .m file in a subfolder? In my case, the name of the subfolder would be "Workspace" and the name of the file in the subfolder would be "constants.m".
Best regards
Benjamin Pommer
Benjamin Pommer on 5 Sep 2022
Actually, I wanted to open and run the file to get the variables in my workspace to further process them in my current file.

Sign in to comment.

Accepted Answer

Rik on 5 Sep 2022
Edited: Rik on 5 Sep 2022
If you want the read the file as text:
You can use a relative path:
You can also include the current working directory explicitly:
If you want to run the m file, that depends on whether it is a function or a script. You need the run function for a script file. For a function you should add the folder to your path and simply call the function.

More Answers (1)

Image Analyst
Image Analyst on 5 Sep 2022
Add it to your path. In your main script, have this line
After that you should be able to call any other scripts or functions in your "Workspace" subfolder or any other subfolders of where your main script lives.
If you want to make that permanent, then also call savepath


Find more on File Operations 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!

Translated by