Search code examples
matlabtext-processingcell-array

Generate Cell Array of Multiple Rows and Columns From Text File


I have a text file formatted like this:

ab cd ef gh ij
ab cd ef gh ij
ab cd ef gh ij

How can I import this data into Matlab so that it is stored as a cell array that is equivalent to this 3x5 cell array:

C = {
    ['ab'], ['cd'], ['ef'], ['gh'], ['ij'];
    ['ab'], ['cd'], ['ef'], ['gh'], ['ij'];
    ['ab'], ['cd'], ['ef'], ['gh'], ['ij'];
    };

I can modify the text file to contain some sort of "end-of-line" indicator if needed. I tried using textscan, but can't figure out how to get it to format the variable as described.

EDIT: Changed the accepted answer; I like the handling of potentially missing data points in Divakar's answer.


Solution

  • See if this works for you -

    c1 = cellfun(@(x) strsplit(x),importdata(textfile_path),'un',0);
    C = vertcat(c1{:})
    

    This basically reads the text data into a cell array which is run through strsplit to split the data into cells and rest is just re-arranging the data to get the desired output.

    Instead of strsplit, you can use regexp there -

    c1 = regexp(importdata(textfile_path),'\s','split'); %// As suggested by Luis!
    

    Bonus stuff

    Let's suppose you have an "irregular" shaped text file i.e. to say, some data is missing along the columns at the trailing positions, something like this -

    ab cd ef gh ij
    ab ry mf pp
    ab ui rt ko bw
    

    In that case, you can use a modified version of the earlier code -

    c1 = cellfun(@(x) strsplit(x),importdata(textfile_path),'un',0);
    lens = cellfun('length',c1);
    
    C = cell(max(lens),numel(c1));
    C(bsxfun(@le,[1:max(lens)]',lens')) = [c1{:}];
    C = C'
    

    The output stays committed to that 2D structure, keeping empty cells for the empty places -

    C = 
        'ab'    'cd'    'ef'    'gh'    'ij'
        'ab'    'ry'    'mf'    'pp'      []
        'ab'    'ui'    'rt'    'ko'    'bw'