How to use relative path to use matlab file in another computer

Illustration
Harsha - 2023-12-27T15:09:07+00:00
Question: How to use relative path to use matlab file in another computer

I have a folder in P:\study\Main. Inside the "Main" folder there is another sub folder called "Sub". There is a m file in main called "main_s.m". This refers another m file called "S2S.m" in "Sub" folder. To do this I had to use addpath('P:\Study\Main\Sub') in the m file. when I copy "Main" in to another computer different drive and execute "main_s.m" it will lead to an error. Is there a way to write a path which can be used in anywhere without altering the code in the m file?  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

You can use .\<folder_name>. The '.\' denotes that the current folder should contain the specified folder. So, having the code in the Main folder, the path should look like this:
 
 
addpath('.\Main\Sub')

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!