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

83 visualizzazioni (ultimi 30 giorni)
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 Commenti
Louise Wilson
Louise Wilson il 16 Lug 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 il 17 Lug 2019
Modificato: Stephen23 il 17 Lug 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).

Accedi per commentare.

Risposta accettata

Michael Madelaire
Michael Madelaire il 16 Lug 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 Commento
Louise Wilson
Louise Wilson il 16 Lug 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!!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by