- 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).
How to make a folder, call functions from it, and then go back to the current directory
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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);
0 comentarios
Respuestas (1)
Jan
el 26 de Feb. de 2023
Editada: Jan
el 26 de Feb. de 2023
Changing the current directory is not useful in stable code, neither to process data nor to run Matlab functions.
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 comentarios
Life is Wonderful
el 24 de Abr. de 2023
Editada: Life is Wonderful
el 24 de Abr. de 2023
Stephen23
el 24 de Abr. de 2023
Editada: Stephen23
el 24 de Abr. de 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."
Source: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html#buwj1nn
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.
Ver también
Categorías
Más información sobre Search Path en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!