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

12 Ansichten (letzte 30 Tage)
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
% 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
[ovf] = check_ovf_(path,params);
% create_new_dir
% add new function
[norm] = check_norm(path,params);
% return to main path aka current_dir
[res] = eval_results(path,params);

Antworten (1)

Jan am 26 Feb. 2023
Bearbeitet: Jan am 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 Kommentare
Stephen23 am 24 Apr. 2023
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.


Find more on Search Path in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by