MATLAB Answers

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

235 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

Show 1 older comment
Louise Wilson
Louise Wilson on 16 Jul 2019
Thank you Stephen, this is really helpful and good to know. I realise now I used fullfile inside my functions but not in the script I'm trying to write to call the functions, so I've amended that now. It's still not clear to me how I would use this approach to call a function in an .m file that's in a different folder to the working directory though?
At the start of my code, I have all of my .avi files I am working on and the two functions I want to use in one folder. I then apply the first function which generates a folder of .png files and in the end, the wd becomes this folder where all the newly generated .png files are. I don't ask the code to do this I don't think? It just happens to move to where the new files are?
This is the function:
function GetFrames(x)
paths=x;
%x is directory where .avi files are
d=dir(fullfile(paths, '*.AVI'));
fileCount=length(d); %number of files to be processed
if ~exist('Frames', 'dir') %create output folder if it isn't there
mkdir('Frames')
end
%% Get Frames
for j=1:fileCount
vidfile=d(j).name; %get name of jth video file
getdate=strsplit(d(j).date); %get date and time of each file
thedate=getdate(1); %take date only
formatOut='ddmmyyyy'; %specify format you want date to be in
filedate=datestr(thedate, formatOut); %date of each file to be used in output
videoObject=VideoReader(vidfile); %read in avi file
folder=pwd; %print working directory to get path to folder where you want new folder to go
outputFolder = sprintf('%s/Frames', folder); %write output files to new 'Frames' folder
%Determine how many frames there are
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
for x=1 : numberOfFrames
frame = read(videoObject,x);
outputBaseFileName = sprintf('-%4.4d.png', x); %number each output file from 0001.png
outputFullFileName = fullfile(outputFolder, strcat(filedate, outputBaseFileName)); %full output filename with folder path, file date, and number
imwrite(frame, outputFullFileName, 'png'); %write output png file
end
end
Then, I want to run my next function on these newly generated files but I'm stuck in the folder of newly generated files and my m.file is in the folder I originally started in. 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? I looked at the help file you attached but it's still not clear to me, sorry if I missed it!!
The function is AddGrids(x) where x is the directory where the files I want to apply the function to are.
Thanks for your help!
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?
Stephen Cobeldick
Stephen Cobeldick 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)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!