Search code examples
matlabloadmatlab-loadpermute

What does this statement mean in MatLab


filename = 'SOMETHING';
eval(['load ' filename]);

eval(['load ' filename '_vid1'])
vid123=permute(vid,[2 1 3]);
size(vid)

eval(['load ' filename '_vid2'])
vid123(:,:,(size(vid123,3)+1):(size(vid123,3)+size(vid,3)))=permute(vid,[2 1 3]);
size(vid)

I know it has to do with loading a file and finding other files with the same name and '_vidX' appended to it, but what does line 7 exactly do/mean?


Solution

  • Carrying on from Daniel's answer, your question is stemming from the fact that you don't quite know how permute works. The goal of permute is to rearrange the dimensions of a matrix but leave the size of the matrix intact. You specify the matrix you want to "permute" as well as a vector that tells you which input dimensions map to the output.

    What permute(vid, [2 1 3]); means is that the second dimension of the input goes to the first dimension of the output. The first dimension of the input goes to the second dimension of the input, and the third dimension stays the same. The effect of this is that vid is a 3D matrix where each 2D slice /frame is transposed while maintaining the same amount of slices / frames in the final output. You are swapping the second and first dimensions which is essentially what the transpose does.

    Therefore, the first load statement loads in your frames through the variable vid, and vid123 originally has some number of frames where each frame is transposed - these correspond to the first video. After this, you are loading in the second video where vid gets overwritten with the frames from the second video. You then add those frames on top of vid123. Therefore, you are simply piecing the frames from the first frame and second frame together - transposed creating one larger video.

    I would highly recommend you resave this so that both videos are separated clearly by different variables, or perhaps have a structure that contains both of the videos together. Having the variable vid being stored in two separate files is problematic.

    Something like this would work:

    load([filename '_vid1']);
    vid1 = vid;
    load([filename '_vid2']);
    vid2 = vid;
    clear vid;
    save videos;
    

    ... or even this would work:

    load([filename '_vid1']);
    s.vid1 = vid;
    load([filename '_vid2']);
    s.vid2 = vid;
    clear vid;   
    save videos;
    

    In the first version, both videos are stored in separate variables vid1 and vid2 and the second version, both videos are saved in one structure.