Right way to insert files paths into code
76 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
paul_dak
el 15 de Jul. de 2021
Comentada: paul_dak
el 20 de Jul. de 2021
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 comentarios
dpb
el 15 de Jul. de 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.
Respuesta aceptada
Rik
el 15 de Jul. de 2021
Editada: Rik
el 15 de Jul. de 2021
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 comentarios
Stephen23
el 15 de Jul. de 2021
Editada: Stephen23
el 16 de Jul. de 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!
Más respuestas (0)
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!