[MATLAB] Processing File Formats

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!

3 thoughts on “[MATLAB] Processing File Formats

  1. I was looking for a regexp switch in matlab – this is a good idea. Heres something that could improve it:

    Instead of checking empty cells and using a loop, do

    reg_result = horzcat(reg_cell{:});

    and then switch on reg_result. This relies on the assumption that each of the possible cases can be matched uniquely, i.e if we match case n, we can not match any other case as well.

    Thanks for this.

  2. Sorry, change the line

    reg_result = horzcat(reg_cell{:});

    to

    reg_result = find(cellfun(‘length’, reg_cell));

    This correctly gives the index of the non-empty element.

  3. Thanks for the comment!

    Your way of finding the cell that matches the cases first reduces the required processing from the original code (namely, sequentially checking if the case is done or not). It’s too bad there isn’t a cleaner way to do this.

    In the weird case that a user has a file named with multiple extensions (i.e. ‘my_weird_video.mov.avi’), then the code for both ‘.mov’ and ‘.avi’ will be executed, which I don’t think would be a huge problem. It is something to be aware of, though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s