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

H
Harsha · Dec 27, 2023 · 2K views
Question
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 PhD Expert
Answered Aug 25, 2026
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')

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!