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

조회 수: 26 (최근 30일)
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);

답변 (1개)

Jan
Jan 2023년 2월 26일
편집: Jan 2023년 2월 26일
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
Life is Wonderful
Life is Wonderful 2023년 4월 24일
편집: Life is Wonderful 2023년 4월 24일
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 2023년 4월 24일
편집: Stephen23 2023년 4월 24일
"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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by