- Changing the current directory in code, or
- Adding folders to the MATLAB Search Path, or
- Moving a function file.
How to change the working directory as code progresses...
83 views (last 30 days)
Show older comments
Louise Wilson
on 16 Jul 2019
Hi all, I have two functions that I would like to run on a folder of files.
The first 'GetFrames(x)' takes a folder of .avi files (specified by path 'x') and outputs them in a new folder within x called 'Frames'.
I then use a second function called 'AddGrids(x)' on these files (x=x/Frames), and produce a new folder within x called 'FramesWithGrids'.
My problem is that for this to work, I need to run the first function, wait until the Frames folder is created, and copy the 'AddGrids.m' file into the new folder before I can run the second function.
I feel like there should be a way to do this without having to copy the .m file manually. I thought it would work by changing the directory but it does not.
Thanks in advance for any suggestions you can offer!
Louise
x='C:\Users\lwil634\Documents\Cameras\practice' %folder where .avi files are
GetFrames(x);
%once this runs we have created Frames folder which contains files we want
%to run next function on, but have to stay in x where .m files are.
b=strcat(x, '\Frames');
cd(b);
AddGrids(b)
%If I copy AddGrids.m into Frames folder the function works and creates the
%next subfolder inside the Frames folder.
AddGrids('C:\Users\lwil634\Documents\Cameras\practice\Frames')
4 Comments
Stephen23
on 17 Jul 2019
Edited: Stephen23
on 17 Jul 2019
"How do I call this function when it's in a different folder without changing the working directory? Do I put something in front of the function name to specify where it is?"
To call a function its file must be on the MATLAB Search Path:
The Search Path simply tells MATLAB where to look for functions. The current directory is implicitly prepended to the Search Path, which is why changing directories lets you run the function. In general if you want to run a function which is not in on the Search Path then you can change the Search Path:
https://www.mathworks.com/help/matlab/matlab_env/add-remove-or-reorder-folders-on-the-search-path.html
As I explained in my earlier comment, data files do NOT need to be on the Search Path: you can always access them using absolute/relative filenames (and this is strongly recommended).
Accepted Answer
Michael Madelaire
on 16 Jul 2019
It is unclear to me where AddGrids is located and why you have to change directory to the Frames folder.
Here are some options:
1). If the problem is that you are in the Frames directory and the function is in another use addPath
x='C:\Users\lwil634\Documents\Cameras\practice' %folder where .avi files are
GetFrames(x);
%once this runs we have created Frames folder which contains files we want
%to run next function on, but have to stay in x where .m files are.
b=strcat(x, '\Frames');
cd(b);
addPath('C:\Users\lwil634\Documents\Cameras\practice') % Path to where AddGrids is located
AddGrids(b)
2). If you want to move the AddGrids function into the Frames subfolder
x='C:\Users\lwil634\Documents\Cameras\practice' %folder where .avi files are
GetFrames(x);
%once this runs we have created Frames folder which contains files we want
%to run next function on, but have to stay in x where .m files are.
b=strcat(x, '\Frames');
cd(b);
copyfile 'C:\Users\lwil634\Documents\Cameras\practice\AddGrids.m' 'C:\Users\lwil634\Documents\Cameras\practice\Frames\AddGrids.m'
AddGrids(b)
More Answers (0)
See Also
Categories
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!