Here’s a recent problem that I’ve come across and found a pseudo-elegant way to solve it.

So I’m writing a MATLAB gui to do some video processing and I want it to be able to process different kinds of video formats, .avi, .mov, etc. But I’ve got this really weird format that it has to process. I don’t want the user to sweat the different formats though — I want them to just choose their file name, hit run, and have things just work. Here’s a snippet of code that I’ve come up with to deal with file formats.

cases = {'.avi', '.mov', '.spl'};
num_cases = numel(cases);
reg_cell = regexp(vid_path_name, cases);

for ii = 1:num_cases
    if isempty(reg_cell{ii})
        continue
    end
    
    switch ii
        case 1
            % Read .avi file
        case 2
            % Read .mov file
        case 3
            % Read .spl file
    end
end

The basic idea is to list the file formats in the ‘cases’ variable, do a regexp on the filename (here, the filename is called ‘vid_path_name’), and then iterate over the regexp output with a switch statement to read in the data. In each of the cases, I can go in now and read in the video file however I want. For the case of the .spl file, it’s dependent upon some custom read code — that is, I can’t use MATLAB’s fancy mmreader function.

Really, it’s just a work-around for the MATLAB switch statement which uses strcmp instead of regexp, which is why I call this code snippet “pseudo-elegant”. Either way, it works, but maybe somewhere out there has a better way to do this? If so, leave a comment!

Advertisements