How to change the working directory as code progresses...

82 views (last 30 days)
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
Louise Wilson
Louise Wilson on 16 Jul 2019
This works
x='C:\Users\lwil634\Documents\Cameras\test'; %path where ..avi files are
GetFrames(x);
addpath(x); %folder where m.files are
cd(fullfile(x, 'Frames'));
AddGrids(fullfile(x, 'Frames'));
%C:\Users\lwil634\Documents\Cameras\test\Frames
...but did you say it would be better if I didn't use cd?
First-I run my first function by specifying the location of the files and the m.files (they are in one folder called test) =x.
Then, I add this path again to where the .m files are.
Then I have to move directory to where the new files that I want the next function to work on are.
This is useful because it means I only have to specify one output, and then I could do a loop to apply it to a series of folders of folders rather than just one? But how could I rewrite it to avoid using cd?
Stephen23
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:
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).

Sign in to comment.

Accepted Answer

Michael Madelaire
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)
  1 Comment
Louise Wilson
Louise Wilson on 16 Jul 2019
Hi Michael-thank you! addpath is exactly what I was looking for! I'm sorry I didn't explain it so clearly, so thank you for providing options to what you thought the solution was. I will do better!!

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!