MATLAB: How to change the working directory as code progresses…

cddirfoldersfunctionsMATLABwd

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')

Best Answer

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)