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.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 8 de Mayo de 2020
Editada: Ameer Hamza el 8 de Mayo de 2020

0 votos

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.

6 comentarios

Russell Senior
Russell Senior el 8 de Mayo de 2020
Editada: Russell Senior el 8 de Mayo de 2020
Works perfect. I thought it would involve the tokens flag, but regular and expressions and I don't really get along, I just don't use them enough. Thanks!
Ameer Hamza
Ameer Hamza el 8 de Mayo de 2020
I am glad to be of help.
Russell Senior
Russell Senior el 8 de Mayo de 2020
Of course, I find one that doesn't fit... Within my set of strings, I have at least one string that only has the name and type token. How do I extract the value by token name? The exact string that gave me an error is:
Original column heading: '(name='/mandySimulation_1/clock/stepCount' type='INTEGER')'
Notice it does not contain unit or quantity tokens.
So, I guess for this I would like to extract:
name = '/mandySimulation_1/clock/stepCount'
type = 'INTEGER'
quantity = ''
unit = ''
Thanks in advance.
Ameer Hamza
Ameer Hamza el 8 de Mayo de 2020
Editada: Ameer Hamza el 8 de Mayo de 2020
In this case, I think that struct is the most suitable way to organize the data. For example
str(1) = "Original column heading: '(name='/mandySimulation_1/clock/simulationTime' type='DOUBLE' quantity='TIME' unit='s')'";
str(2) = "Original column heading: '(name='/mandySimulation_1/clock/stepCount' type='INTEGER')'";
matches = regexp(str, '(\w*)=''([^\s''.]*)''', 'tokens');
S(numel(matches)) = struct('name', "", 'type', "", 'quantity', "", 'unit', "");
for i=1:numel(matches)
match = matches{i};
for j=1:numel(match)
S(i).(match{j}(1)) = match{j}(2);
end
end
Result:
>> S(1)
ans =
struct with fields:
name: "/mandySimulation_1/clock/simulationTime"
type: "DOUBLE"
quantity: "TIME"
unit: "s"
>> S(2)
ans =
struct with fields:
name: "/mandySimulation_1/clock/stepCount"
type: "INTEGER"
quantity: ""
unit: ""
Adapt it according to your requirement.
Russell Senior
Russell Senior el 11 de Mayo de 2020
Thanks so much! I hope wherever you are working, they are paying you well!
One minor correction:
S(i).(match{j}(1)) = match{j}(2);
Should be
S(i).(match{j}{1}) = match{j}{2};
Then it works 100% perfect. Nicely done, and thanks again!
Ameer Hamza
Ameer Hamza el 11 de Mayo de 2020
I am glad to be of help. Thanks for your kind remarks :)
In R2020a, both match{j}(1) and match{j}{1} works. Maybe this is something related to MATLAB releases.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 8 de Mayo de 2020

Comentada:

el 11 de Mayo de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by