0

Say I have a list of directories:

/users/david/patients/patientID/Day
/users/david/patients/patientID/Daya/HospID_x/Data
/users/david/patients/patientID/Dayb/HospID_y/Data
/users/david/patients/patientID/Dayc/HospID_z/Data
/users/david/patients/patientID/Day
/users/david/patients/patientID/Daya/HospID_x/Data
/users/david/patients/patientID/Dayb/HospID_y/Data
/users/david/patients/patientID/Dayc/HospID_z/Data
/users/david/patients/patientID/Day
.
.

The data is mixed, contains images, vectors, text and so on. I have generated a list of folders which contains data which I want to perform operations on. After working on these data types in the my script, I have generated a bunch of variables in the workspace. The variables are overwritten in the next cycle. I want to create a struct file which contains these variables or results. This means, a patient will have several DaysID under a patient ID and varying HospID under DaysID.

6
  • Could you make a tree structure to explain this a bit better, you think? I have some trouble finding which field are values and which are titles. Commented Jul 30, 2015 at 9:06
  • Here is the tree structure: imgur.com/OEHqfT6, thanks. What stumps me is getting the fields in the struct visited once and the values created in the fields. Commented Jul 30, 2015 at 10:08
  • What would you say about nested struct array instead of cells? Further, is performance an issue here? If performance is an issue, you should use a nested for loop or while loop. Since the structure is fixed (the dept of the nested for loop is deterministic) this is easy. Let the first loop be Patient and go deeper each loop. The name of the fields can be set in this fashion, t='DayA'; q.(t) = 'foo';. Try this. It is hard to provide code when I do not know exactly what your variables looks like. With nested struct array, I mean an array of structs containing other structs or struct arrays Commented Jul 30, 2015 at 11:07
  • Please find the code above, the list of folders follows as above i.e.: Commented Jul 30, 2015 at 11:44
  • Ok, thanks, I think I begin to get a clear view of the problem now. I understand that you want to name the struct from the folder system. Just one question more. The folders /users/david/patients/patientID/Day are these empty folders wtih that exact name (maybe except for that the patient id which is unique)? Commented Jul 30, 2015 at 12:17

1 Answer 1

1

Ok, the code took a time to get down. Further, the code is a bit hard coded in some places, so you may want to clean up a bit. The main issue is that I did not know the exact format of your Data files. Also, the code does not include cases for when the field is empty for more than the field Day__ since this was how you presented the data. This means that you will have to do some things to customize.

1) Rename the paths
2) Add if-statements for empty if needed
3) Edit the inner loop to work with your data.

The code should be clear enough. I have made a code based on for loops where you step down one level at the time. I have not used your exact hierarchy though, but a similar. If this does not work, try to comment ans explain why. The code,

function dummies = test()

basedir = 'C:\Users\username\Documents\MATLAB\dummies';
level1 = dir(basedir);
dummies=[];

for k = level1(3:end).'
    level2 = dir(fullfile(basedir, k.name));
    for l = level2(3:end).'
        level3 = dir(fullfile(basedir, k.name, l.name));
        if length(level3) < 3
            dummies.(k.name).(l.name) = [];
        else
            for m = level3(3:end).'
                level4 = dir(fullfile(basedir, k.name, l.name,m.name));
                for n = level4(3:end).'
                    dat = load(fullfile(basedir, k.name, l.name,m.name,n.name));
                    dummies.(k.name).(l.name).(m.name) = dat;
                end
            end
        end
    end
end 

It is possible to implement this with an recursive solution as well, but Matlab is not so good on recursion, so I avoid this for this example.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Patrik! Please let me try this out for a bit.
I am not sure I understand what you are doing here. I do already have a list of files. Which is accessed sequentially, a key reason to use that is to avoid the mess wherever the files are located, for instance on mac platform or separate servers you have the '.DS_Store' folders, or xml files and so on. The only place I need to need with data is in the location folder. I just dry running at the moment to return a struct file with empty fields which i can populate. Please let me know if I am unclear.
@Rigusorio I thought the whole idea of the exercise was to import the data and store it in a struct array. The code provided will (with some modification to fit your data) give you a struct filled with your data. But if you want an empty struct (which I do not know why you want when you can have a filled to almost the same cost), just leave the field where I add the data empty (dummies.(k.name).(l.name).(m.name) = []). If you are after something else, please edit the question and specify your needs better.
Thanks! The idea is go to a folder (last level) where data is 'do a bunch of stuff', store the results in a fields but allow us trace back where the results came from i.e. by the location.
Thanks! The idea is go to a folder (last level) where data is 'do a bunch of stuff', store the results in fields but allow us trace back where the results came from i.e. by the location. Therefore we have a struct file after processing which goes like: 'dummies.(k.name).(l.name).(m.name).datatypes' I use the list of folder names to go to where the data is, do stuff on the way out of the folder or in. I create struct file which hold the results - which stumps me at the moment. Its is naming routine or constructing addresses for where I find my files that I am after. Thanks Patrik!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.