0
votes

I am a Matlab beginner, as will soon be very obvious. I am trying to assemble an cell array that has a single column of filenames.

I have multiple sessions. Each session should have 56 filenames (but some could be short or long, so I'd honestly prefer a solution that wouldn't break on encountering a short session). I need to loop over sessions and append the names in each subsequent session to my cell array, so that after two sessions the dimensions are 112, 1.

In other words, I'd like an array that went:

P =

/data/session1/dvol1.img
/data/session1/dvol2.img
...
/data/session1/dvol56.img
/data/session2/dvol1.img
/data/session2/dvol2.img
...
/data/session2/dvol56.img

and so on if there are more than two sessions.

The function I have that finds the filenames in the session is spm_select. It returns a char array of all the files in a directory that match a regular expression, in this case, 56 files for each session directory.

(I recognize my question is very similar to the question here: Using loops to get multiple values into a cell but I couldn't figure out an answer to my question since that person is only trying to append a single value at a time.)

I have tried a lot of things that haven't worked.

This:

data_path = '/foo/bar/';
subjects = {'test1'};
sessions = {'session1' 'session2' };
for i=1:numel(subjects)
    clear P
    P=cell(56*numel(sessions),1); 
    for j=1:numel(sessions)
        P{(j-1)*56+1} = spm_select('FPList', fullfile(data_path,subjects{i}, sessions{j}), '^d.*\.img$');
    end   
end

generated a cell array that was 112x1, but had a first element that was 56x57 char array, that is, the filenames of all files in my first session directory, and none of them from the second.

I'm not sure how useful it would be to recapitulate every wrong-headed thing I've done, so I won't.

Thanks in advance for your help.

Editing to include sample output from spm_select by request:

>> output = spm_select('FPList', fullfile(data_path,subjects{i}, sessions{j}), '^d.*\.img$')

output =

/home/katie/Desktop/sample/test1/run_1L3/draghf000001.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000035.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000069.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000103.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000137.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000171.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000205.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000239.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000273.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000307.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000341.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000375.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000409.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000443.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000477.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000511.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000545.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000579.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000613.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000647.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000681.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000715.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000749.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000783.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000817.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000851.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000885.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000919.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000953.img
/home/katie/Desktop/sample/test1/run_1L3/draghf000987.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001021.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001055.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001089.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001123.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001157.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001191.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001225.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001259.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001293.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001327.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001361.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001395.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001429.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001463.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001497.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001531.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001565.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001599.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001633.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001667.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001701.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001735.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001769.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001803.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001837.img
/home/katie/Desktop/sample/test1/run_1L3/draghf001871.img

>> class(output)

ans =

char

>> size(output)

ans =

    56    57

>> 

Edit: Ok, problem solved. Here is the code I eventually used:

data_path = '/foo/bar';
subjects = {'test1'};
sessions = {'session1' 'session2' };

output={};
for i=1:numel(subjects)
    for j=1:numel(sessions)
        files=spm_select('FPList', fullfile(data_path,subjects{i},sessions{j}), '^d.*\.img$')
        f_c=cellstr(files);
        output=vertcat(output,f_c);
    end
end

I think the answer to how "do you get a char array to append to a column cell array vertically" is convert it to a cell array and use vertcat.

1
Does your spm_select really return a char array, or a cell array of strings? (file names may have different lenghts). Can you edit the question to post an example of spm_select's output? - Luis Mendo
If all you want is: the filenames in all the sessions folder. Then this will work for you. Call it as fileList = getAllFiles(data_path); - Autonomous
Thanks Luis, I did so. Parag, I I don't want all the filenames. I only want the ones that match the regular expression. - Katie
1. In your spm_select function, where is the mention of regular expression? 2. Why do you write P{(j-1)*56+1} where j takes on only two values: {1,2}. 3. How can you get a 112x1 cell array? Put j=2 and you get 57, so you can get at most 57-element long cell array. Can you do whos P and paste the output here? - Autonomous
1) is regular expression not the term for this: '^d.*\.img$'? Sorry if I'm not using the right words 2) I'm not defending the logic of that. I thought maybe Matlab would stick the array in the first slot I named. I honestly was just giving it as an example of something I've tried. I did try something that made more sense to me: P{(j-1)*56+1:j*56} The right hand side of this assignment has too few values to satisfy the left hand side. Error in arr (line 8) P{(j-1)*56+1:j*56} = spm_select('FPList', fullfile(data_path,subjects{i}, sessions{j}), '^d.*\.img$'); - Katie

1 Answers

0
votes

You can try this code:

function output=file_list(path)
output={};
subjects=dir(path);
for a=3:length(subjects)
    sessions=dir(fullfile(path,subjects(a).name));
    for b=3:length(sessions)
        files=dir(fullfile(path,subjects(a).name,'/',sessions(b).name,'/*.img'));
        f_c=struct2cell(files);
        f=f_c(1,:)';
      output=vertcat(output,fullfile(path,subjects(a).name,'/',sessions(b).name,'/',f));
    end
end

One drawback of this code is that the size of output grows inside the loop. Here is an example:

path='/home/naveen/Desktop/example/'; % path is the main directory in which the data of 
                                      % subjects is stored in sub directories. 
output=file_list(path)

The output is:

output = 

    '/home/naveen/Desktop/example/subject_1/session_1/lipo2.png'
    '/home/naveen/Desktop/example/subject_1/session_1/lipo_6.png'
    '/home/naveen/Desktop/example/subject_1/session_1/lps_4.png'
    '/home/naveen/Desktop/example/subject_1/session_2/ltx_2.png'
    '/home/naveen/Desktop/example/subject_1/session_2/ltx_2_1.png'
    '/home/naveen/Desktop/example/subject_1/session_2/ltx_2_3.png'
    '/home/naveen/Desktop/example/subject_1/session_2/ltx_4.png'
    '/home/naveen/Desktop/example/subject_1/session_2/ltx_6.png'
    '/home/naveen/Desktop/example/subject_2/session_1/lipo2.png'
    '/home/naveen/Desktop/example/subject_2/session_1/lipo_6.png'
    '/home/naveen/Desktop/example/subject_2/session_1/lps_4.png'
    '/home/naveen/Desktop/example/subject_2/session_2/ltx_2.png'
    '/home/naveen/Desktop/example/subject_2/session_2/ltx_2_1.png'
    '/home/naveen/Desktop/example/subject_2/session_2/ltx_2_3.png'
    '/home/naveen/Desktop/example/subject_2/session_2/ltx_4.png'
    '/home/naveen/Desktop/example/subject_2/session_2/ltx_6.png'

Hope this works for you. Please note that in the inner most for loop you have to change the file extension while using for your purpose.