Clear Filters
Clear Filters

How to make a folder, call functions from it, and then go back to the current directory

23 views (last 30 days)
Hello,
I'm looking for help with creating a directory structure and calling a function from a new directory.
Could you kindly assist with creating the directory, adding a path to the function, calling the function from new directory folders, and storing the results in a result location?
Below is a fictitious style flow that I believe I am searching for.
Thank you very much.
current_dir = pwd;
[ret] = add_function(params);
% append and create_check_filter directory
current_dir/check_filters/
% add overflow & saturation functions in the check_filters folder and call
% check_ovf(), check_sat() functions from add_function ()
[ovf] = check_ovf(path, params);
[sat] = check_sat(path,params);
% return to main directory
% perform multiplication operation
[ret] = mult_function(params);
% append and creat norm_filter filter folder in the current directory
current_dir/check_filters/
[ovf] = check_ovf_(path,params);
% create_new_dir
current_dir/check_filters/norm_filter
% add new function
[norm] = check_norm(path,params);
% return to main path aka current_dir
[res] = eval_results(path,params);

Answers (1)

Jan
Jan on 26 Feb 2023
Edited: Jan on 26 Feb 2023
Changing the current directory is not useful in stable code, neither to process data nor to run Matlab functions.
  • The addpath command adds a folder to Matlab's path, such that the included M-files are called automatically.
  • Use the absolute path of a data file using fullfile(folder, filename).
Note that any callback of a GUI or Timer can call cd() and change the current folder unexpectedly. Therefore it reduces the chance of bugs to avoid relative path names in general.
  7 Comments
Life is Wonderful
Life is Wonderful on 24 Apr 2023
Edited: Life is Wonderful on 24 Apr 2023
This is how it should or can be done!!
pwd = 'C:\';%in the current path
path(path,[pwd '\folderToswitch;']);% path to move from current location to dest location
folderToswitch
Stephen23
Stephen23 on 24 Apr 2023
Edited: Stephen23 on 24 Apr 2023
"The need for this analysis arises when one function needs to capture data while another needs to store it in a folder."
The need to access data files in different folders does not require changing the current directory. In fact, changing the current directory is slow and makes debugging harder. Best avoided. The MATLAB documentation specifically warns against your approach: "Avoid programmatic use of cd, addpath, and rmpath, when possible. Changing the MATLAB path during run time results in code recompilation."
As Jan correctly wrote: the efficient, recommended, reliable approach is to use absolute/relative filenames. All MATLAB functions which import/export data files accept absolute/relative filenames. You should use absolute/relative filenames to access your data files. So far there is nothing in your explanation that prevents you from using absolute/relative filenames to access your data files.
Keep your code and data separate. Do not copy code to new folders just to run it. Keep the code in one location and use absolute/relative filenames to tell your code where the data files are stored.
Do not change directories just to access data files.

Sign in to comment.

Categories

Find more on Search Path in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!