Right way to insert files paths into code

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
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
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
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
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.
Tnx guys,
my files are mostly .txt and .csv file located on a cloud drive folder, with a loactions that look like: 'G:/my_folde/.../my_file'.
I have my matlab project folder and it has many subfolders, all my files are spread within those subfolders.
So each .m file contain lines like that:
table_path = ['G:\My Drive\folder1\folder2\folder3\MATLAB\'...
'folderA\my_file.xlsx'];
and i whish to avoid such lines that may create "data source" issues when folder names are changed.
in addition, it looks wrong to me to define many locations at each new script im writing.
paul_dak
paul_dak am 15 Jul. 2021
my idea was to write a function file, that recieves a string of the subfolder name that is located in the project directory, and the function will return its full path.
That way in case of folder changing, i will only have to edit that one function. wonder if there is a better way.
dpb
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Rik
Rik am 15 Jul. 2021
Bearbeitet: Rik am 15 Jul. 2021

1 Stimme

Personally I use a function like the one below to semi-hard-code the paths. The context for this is that I have a synced folder, but on one system it is 'C:\User\#me#\#sync software#\project', while on another it is 'B:\#sync software#\project'.
If you have any way to automatically determine the path of the network drive, you can use a function like this to retrieve all sub-directory paths.
function p=places(id)
%return value has no trailing filesep
root_project_folder=fileparts(fileparts(mfilename('fullpath')));
switch id
case 0
p=root_project_folder;
case 1
p=fullfile(root_project_folder,'scripting');
case 2
p=fullfile(root_project_folder,'image_output');
case 3
p=fullfile(root_project_folder,'SPSS_output');
otherwise
error('unknown id')
end
end
This way, I can move the project folder around without having to rename anything, and if I need a static path, I have only 1 place need to edit.

2 Kommentare

Stephen23
Stephen23 am 15 Jul. 2021
Bearbeitet: Stephen23 am 16 Jul. 2021
+1 regardless of the particular approach, I think the key is described in this answer: the path/s should only be defined in one location. Exactly how it works is a matter of taste: all of the functions/classes could pass the path as input/output argument, read some config file, or use something like what Rik shows above. Relative filenames might be very useful in this situation.
In any case, definitely avoid hard-coding the path into every script, function, or class!
paul_dak
paul_dak am 20 Jul. 2021
Tnx, looks like what i need :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2021a

Gefragt:

am 15 Jul. 2021

Kommentiert:

am 20 Jul. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by