MATLAB: Help parsing a string

MATLABparsingstrings

I need some help parsing a char array, hopefully someone has an idea. Matlab loads my table and the table header is not matlab compliant, so it creats a char array that is a property of the table column, and I need to parse that to get the proper table header name.
One of the char arrays is Original column heading: '(name='/mandySimulation_1/clock/simulationTime' type='DOUBLE' quantity='TIME' unit='s')'
So I need to create 4 variables, name, type, quantity, and unit. Each should have the subsequent value per the string. I tried using sscanf, but I can't figure out how to use it. Any suggestsion? I would like the name to keep the "/" characters because I will parse those later. It's a little easier because the / can be a delimiter.
The char arrays I'm working with are not the same length, but they always have the name, type, quantity, and unit variable name. Here is an example of another char array:
Original column heading: '(name='/mandySimulation_1/engine_1/longBlock_1/shortBlock_1/cylinderBlockAssembly_1/cylinder_1/ringLeakage_1/referenceDiameter' type='DOUBLE' quantity='LENGTH' unit='m')'
Thanks in advance for any help you can give.

Best Answer

  • Try this
    str = "Original column heading: '(name='/mandySimulation_1/clock/simulationTime' type='DOUBLE' quantity='TIME' unit='s')'";
    name = regexp(str, '=''([^\s''.]*)''', 'tokens');
    Result
    >> name{1}
    ans =
    "/mandySimulation_1/clock/simulationTime"
    >> name{2}
    ans =
    "DOUBLE"
    >> name{3}
    ans =
    "TIME"
    >> name{4}
    ans =
    "s"
    It will work, as long as the strings are in the specified format.