Search code examples
regexmatlabtokenkey-valuematlab-struct

Can regexp return key/value pairs in a structure?


Let's say I have a character array with key/value pairs:

ch = sprintf('name: John\nsex: M\n')
ch =
    'name: John
     sex: M
     '

This is just a sample. The actual data is in a file and has many pairs. I can use regexp to get tokens, then use a for loop to assign them to a structure:

lns = regexp(ch, '(\w*):\s(.*?)\n', 'tokens');
for i = 1:numel(lns)
    myStruct.(lns{i}{1}) = lns{i}{2};
end

myStruct = 

  struct with fields:

    name: 'John'
     sex: 'M'

Is there a simpler way to achieve this, like using regexp(ch, expr, 'names')?


Solution

  • You can avoid the for loop by collecting your key/value pairs into a single cell array and then passing the contents of that as a comma-separated list to struct:

    args = [lns{:}];
    myStruct = struct(args{:});
    

    And the output:

    myStruct = 
    
      struct with fields:
    
        name: 'John'
         sex: 'M'
    

    Alternatively, you could avoid the use of regexp and use textscan to read from the file, which may improve performance:

    strs = textscan(fileID, '%s', 'Delimiter', ':');
    myStruct = struct(strs{1}{:});