Right way to insert files paths into code
Ältere Kommentare anzeigen
Hi there!
Im working in the data science world, and wonder what is the best way to insert a file path into a code.
Since i have many scripts and classes, and each of them contain folder/files paths within the code (i.e. reading data from server - G:/My_folder/.../my_file), im finiding it problematic when i want to change folder names or to reorginize my directories.
Do you have any wise suggestions how to keep my code rebust enought so this directories changes wont impact it in future.
Tnx!
7 Kommentare
Devanuj Deka
am 15 Jul. 2021
You can try using addpath(folderName1,...,folderNameN), which adds the directories folderName1,...,folderNameN to the MATLAB search path.
Rik
am 15 Jul. 2021
Do you want to actually add folders to the Matlab path, or is your question about how to set up paths you data in a flexible way?
Devanuj Deka
am 15 Jul. 2021
I think I understood your question now. You want to know if there is a solution for the code so that renaming/moving the folders that contain the required data will not affect the execution.
I think that after each time you move/rename the directories, the code will have to be manually edited to reflect the new file/folder paths.
Unfortunately I don't think there is any solution/workaround to this problem. And if there is, I don't know of it.
dpb
am 15 Jul. 2021
If a script or function needs data that may come from various sources, probably it would be best to have a higher-level routine that calls it that either passes the location and specific filename to the routine or handles the task of getting the data itself and then pass it to the routine.
What the best way to handle obtaining those paths and saving and/or retrieving them would depend mostly on how they are determined in the first place -- which we have no information on how you do locate them so not really possible to make any specific recommendations.
paul_dak
am 15 Jul. 2021
paul_dak
am 15 Jul. 2021
dpb
am 15 Jul. 2021
I personally detest building such complex trees but recognize there may be reasons for such deep nesting--but in 40 years consulting I've never come across a case I thought I needed or was well served by more than a couple deep.
That aside, this again is a case of "why is the folder name going to change?" and what's going to determine it needs to change and how is that determined and what sets the new name?
If these data are coming from an external source, then they control the start, not you and you need a database structure to handle that if more than one.
Then, looking at the sample above, presuming that you are creating this nested structure from results, if you maintain the same structure under the top level as I would strongly suggest, then you can use relative addressing from the root directory and never change anything except the root and not even have to code the absolute names at all.
Or, if you have a naming scheme for the subdirectories, similarly, build those dynamically and again only keep the root name.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Search Path finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!