Search code examples
matlabvectortext-parsingchars

Putting a line of chars from a txt-file in a vector in Matlab


I have a txt-file which looks like this

a e e a a i
i i a e u i i a
a i e i i i a i 

and I want to put each line in a seperate vector (every char into one field, of course). The lines have diffrent lengths. Seems to be easy with numeric content. But how can I do this with a line of chars?

My way of choice right now would be to transfer the chars into numbers and use dlmread for the job. Any better ideas?

Thanks.

EDIT: Found a solutuion just by my own. But the response here seems a lot more straight forward.

fname =(['vokale/',si,'.txt']);  
         disp (['loading ...',fname]);
            fid = fopen(fname);           
            [vowels] = textread(fname, '%c');

s = textscan(fid,'%s','Delimiter','\n');
s = s{1};
b1_cell=textscan(s{1,1}, '%s');
b1=b1_cell{1,1};
b2_cell=textscan(s{2,1}, '%s');
b2=b2_cell{1,1};
b3_cell=textscan(s{3,1}, '%s');
b3=b3_cell{1,1};
b4_cell=textscan(s{4,1}, '%s');
b4=b4_cell{1,1};
b5_cell=textscan(s{5,1}, '%s');
b5=b5_cell{1,1};

Solution

  • First, obtain the lines in a cell array using textread:

    C = textread(filename, '%s', 'delimiter', '\n')
    

    Then you can use regexp to split each line into chars:

    result = cellfun(@(x)regexp(x, '\w+', 'match'), C, 'Uniform', false)
    

    The result should be a cell array of cell arrays of chars:

    result = 
        {'a'    'e'    'e'    'a'    'a'    'i'}
        {'i'    'i'    'a'    'e'    'u'    'i'    'i'    'a'}
        {'a'    'i'    'e'    'i'    'i'    'i'    'a'    'i'}
    

    Alternatively, if you're handling only space-delimited letters (that is, one character only), you can store each line as a string (equivalent to an array of chars in C). To do that, just remove the spaces. For example, use regexprep:

    result = cellfun(@(x)regexprep(x, ' ', ''), C, 'Uniform', false)
    result = 
        'aeeaai'
        'iiaeuiia'
        'aieiiiai'
    

    For instance, to access the e in the second line, you can write result{2}(4).