Can I generalize a folder lookup location so that data can be accessed in more than one way?
6 views (last 30 days)
Show older comments
I'm finding this issue difficult to describe, so bear with me...
So a colleague created a structure that looks for files in specified location, for example if I call the structure.get_image() function, it looks in folder '/media/Server/user/...'
However, the data itself has been copied to different servers and is accessed from different workstations (sometimes with different operating systems), and not all of those workstations and servers have the same file structure. For example, in one it may need to look in '/data/DifferentServer/user...' - everything past the server name should be the same for everyone and every type of access, but not so with the server name itself and what comes before it. It may also need to look in a windows computer at a location like Z:\media\Server\user\...' which possibly poses a different problem.
So my question is, is there a way to generalize the folder location to work in all of these cases?
Structure example (omitted some things, but all of the omitted things are universal regardless of how the data is accessed):
algorithm: 'omitted'
parameters: [1×1 struct]
nJobs: 2
comment: []
folder: '/media/Cube/omitted/' % The problem
refScan: 1
uuid: (leaving this out)
patient: []
study: [1×1 study]
sliceFolder: '/media/Cube/omitted/omitted/' % Also the problem
% There is a lot more to these structures, but I think this gives the idea
Error received if accessed from a server that instead has the folder located at '/data/Sphere/omitted/':
Cannot find file
"/media/Cube/omitted/01_deformed.nii".
Currently people have to go in and do this if they want to use the data from another location:
set(thing.subThing,'folder','data/Sphere/omitted');
set(thing.subThing,'sliceFolder','data/Sphere/omitted');
% About 5 more things to set in addition to these, but you get the idea
And then they can't save those changes or else it may mess it up for other people accessing the data. And sometimes we want to work with dozens of these structures at once so we would have to do these sets for each one (and the "omitted" part is different for each one, so it can't be looped). Ideally the folder location would be instead something like:
folder: '.../omitted/'
% Same for sliceFolder and all the other paths
Where the "..." is determined based on where "omitted" is actually located for that user.
Is this sort of generalized pointer possible?
Apologies again for how difficult this has been for me to explain, please let me know if I can provide any additional clarification!
6 Comments
Steven Lord
on 10 Aug 2023
So to write paths that work on both, use /.
To write folder paths that work on Linux, Windows, and Mac use fullfile. Or if you can't for some reason, use filesep.
Answers (1)
Jan
on 10 Aug 2023
The core of the problem is a messed up storage of files in a bunch of folders. Collecting the files in one shared folder would be much cleaner. A database is even better to store a bunch of data.
It is not hard to write a workaround: The folders needed by a user are defined in a function e.g. called "PersonalDataFolder" in the personal user path. Then calling the sharded function "FindFile" requests the list of the folders and searchs for the specific file in the set of given folders:
function File = FindFile(FileName)
FolderList = PersonalDataFolder(); % E.g. {'/media/Server1/user1', '/media/server2/user1'}
File = '$File not found$';
for iFolder = 1:numel(FolderList)
aFile = fullfile(FolderList{iFolder}, FileName);
if isfile(aFile)
File = aFile;
break;
end
% Alternatively: Check the other folders also to find ambiguities
end
end
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!