Model Portability - Troubleshooting Directory Path Error

1 view (last 30 days)
Hello.
I have a MATLAB model that runs only if my current working directory is set to its saved file path. I should note that the model calls a bunch of external files (both MATLAB custom functions and files completely unrelated to MATLAB), all contained within the same folder. I'd like to correct this problem, allowing me to just copy and paste all the folder in whatever other directory, or computer for that matter, and still be able to run the program. What kinds of commands would you guys suggest I investigate? That would be really a great help.
Thank you.

Answers (3)

Joseph Cheng
Joseph Cheng on 7 Oct 2015
Edited: Joseph Cheng on 7 Oct 2015
You probably want to look at the following:
pwd %get current working directory
addpath %add a path to matlab's list of places to search function/files
genpath %get subfolders, don't really think you need this but there it is
cd %change working directory
%folder/file navigation
uigetfile/uiputfile/uigetdir..... etc %user inputs
fullfile() %combine folder and file path to a full path
so depending on what you mean by "working directory is set to its saved file path" you can build the working directory and specified folders based relative to the initial working directory and then the uigetfile/uiputfile/etc in conjunction with fullfile() lets you write to a specific folder location.

Vicente
Vicente on 7 Oct 2015
Thank you for your suggestions. Adding a cd(pwd) line to the code helped me get rid of the warnings and error messages, but a problem remains. My model calls a MATLAB custom function, which was originally coded in C script, and such function is responsible for computing a series of arrays. It worked perfectly when my current working directory was set to the the directory where all the files were originally saved. But now that I want to add portability to my model, how can I make it so that the calling of such custom function is working even though I don't always have the same current working directory? Is that possible to be done without altering the MATLAB custom function? Because I wasn't even the one to design it, and I don't have it's compiling code at the moment. I hope there is a way around it. Thank you again.
  2 Comments
Vicente
Vicente on 7 Oct 2015
This function reads a property file, completely unrelated to MATLAB, and by calling it within the .m script and by assigning output variables, it executes a series of calculations necessary for my work. There are no further error messages, but when the current working directory is not set to the save folder, the function doesn't find the property file it needs, and the computations come out blank. So, is there a way around it anyways?

Sign in to comment.


Vicente
Vicente on 7 Oct 2015
This function reads a property file, completely unrelated to MATLAB, and by calling it within the .m script and by assigning output variables, it executes a series of calculations necessary for my work. There are no further error messages, but when the current working directory is not set to the save folder, the function doesn't find the property file it needs, and the computations come out blank. So, is there a way around it anyways?

Community Treasure Hunt

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

Start Hunting!