Time difference taken between two methods: changing folder manually and adding path to Matlab search directory

3 vues (au cours des 30 derniers jours)
I recently observed this effect. I have the data to be loaded in another directory. So I usually use "addpath" command. But for a change I tried to do it manually and I observed a huge time difference.
Example:
Addpath method ran x4 times
tic
addpath ('C:\MATLAB\2D\data')
load x.dat;load y.dat;load z.dat;
toc
Elapsed time is 0.156776 seconds.
Elapsed time is 0.147188 seconds.
Elapsed time is 0.158762 seconds.
Elapsed time is 0.172729 seconds.
Manually changing directory method ran x4 times
tic
current_folder=pwd;
cd ('C:\MATLAB\2D\data')
load x.dat;load y.dat;load z.dat;
cd(current_folder);
toc
Elapsed time is 0.074755 seconds.
Elapsed time is 0.076723 seconds.
Elapsed time is 0.074736 seconds.
Elapsed time is 0.078758 seconds.
Definitely manually changing folder method is twice as fast as addpath method.. Can anyone try to explain why this is happening?
  11 commentaires
Stephen23
Stephen23 le 10 Juil 2021
Modifié(e) : Stephen23 le 10 Juil 2021
@Saunok Chakrabarty: most important to understand: the Search Path is just a list of folders where MATLAB code is saved:
Note: where code is saved. Not data.
When to use:
  • CD: at the command line, when you want to change what directory you are currently working in.
  • ADDPATH: when you have folders of MATLAB code (e.g. apps or downloaded toolboxes) that you want to use.
  • FULLFILE: whenever you write code that needs to access (i.e. read/write) data files.
PersonalIy I rarely use CD/ADDPATH, not because I don't change the current directory or the Search Path, but simply because the MATLAB IDE has buttons or drop-down menus or right-click context options that perform both of these (and much more). So I prefer using the IDE, rather than command line commands. But you can use whatever suits you best.
"Should I store my data and functions/scripts in separate directories?"
Yes.
In general keep your code folders neat and focussed, i.e. with only code files and anything closely related.
Data files can be saved anywhere appropriate, and then you just need to use FULLFILE to access them. This is more efficient and makes debugging easier.
Walter Roberson
Walter Roberson le 10 Juil 2021
Unfortunately, MATLAB does not have a data search path.
I would point out that it is not productive for users to have to know where Mathworks has moved camerman.tif or rhinos.avi this release: it is useful for them to be able to just read from the appropriate file without having to search for it.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by