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

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);

Antworten (1)

Jan
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

The need for this analysis arises when one function needs to capture data while another needs to store it in a folder. In order to process the results and separate out the good from the bad, I must permit the folder to be created and then read the stored results using analysis scripts before making a decision.
Therefore, I was describing the code using folder filters so that I could troubleshoot the problems and outcomes without having to re-start the system which is consuming a lot of time.
I do not see any reason to restart the system. Please explain, in which situation such a restart (of Matlab or the computer?) seems to be required.
I'm talking about restarting the system with a good-vs-bad data gathering division.
You can use analysing the video streaming as a basic example to practise frame analysis.
One must analyse the video frames unless they have a fairly accurate estimation of the problem in the stream.
This indicates that a mechanism is required to separate the excellent frame from the defect.
While the full video capture may have several hundred frames, I am anticipating no more than 20 to 30 bad frames of data for analysis.
This means that after I discard the excellent frames, I must place the defective ones in a different area for careful examination.
@Life is Wonderful: "I'm talking about restarting the system with a good-vs-bad data gathering division." - I do not understand, what this means. What ist "the system" and what is the reason to "restart" it?
I still do not see a connection to the question concerning adding folders to the path.
@doc:@Jan
I'm talking about restarting the system with a good-vs-bad data gathering division.
Sorry for the delayed reply;
I had to restart the matlab programme, which takes a long time to process the data before writing it to the system disc, and then I read the picture to separate the good and bad data gathering division.
Please keep in mind that I am not recommending that you restart the data collection tool, such as a camera or image acquisition device or my computer.
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
"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.

Kategorien

Produkte

Version

R2022b

Gefragt:

am 26 Feb. 2023

Bearbeitet:

am 24 Apr. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by