Using regexp to parse plot's LineSpec

5 visualizaciones (últimos 30 días)
Modal Analyst
Modal Analyst el 23 de Sept. de 2022
Comentada: Walter Roberson el 7 de Oct. de 2022
Hello, I'm trying to parse the LineSpec string used in plot(...,LineSpec).
LineSpec specifies the plot line's style/marker/color, for example "-or". Some of the possible options are:
  • Style: - | -- | .- | -. | :
  • Marker: [o+*._|sd^v><]
  • Color: [rgbcymkw]
So far I had some success in regexp with
regexp(str,'((-[-|.]?)|:|(.-))?+[o+*._|sd^v><]?+[rgbcymkw]?','match','once')
and
regexp(str,'^(?:([rgbcymkw.:-*sodv^><][^.]*)(?!.*\1))+$','match','once')
or by splitting and erasing the string at each stage (not an elegant solution). However, this method parses also words, is not strict against repetitions and fails by changing the order.
I am trying to split a LineSpec into its three components, but just having regexp recognize a string as a valid LineSpec would be great. Would it possible to do this with a regexp pattern?
Thank you in advance.

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Sept. de 2022
Editada: Walter Roberson el 24 de Sept. de 2022
Shapes and colors in linespec may be given in full or in any leading prefix of the word, and the parts can be run together with no delimiter. For example 'greens' is green colour and square marker, and 'diacy' is cyan diamond.
There is overlap between the color codes and the names of the shapes, so you have to contend with 'square' versus 'squared' vs 'squarer'. The rule appears to be that a valid name is consumed as long as possible, and parsing picks up again from there. So 'squarer' parses out 'square' and leaves it positioned for parsing 'r' for red, but 'squared' does not "back up" to split as "squa" and "red". 'square' + 'd' is invalid because square and d (diamond) are both shapes. 'squareg' and 'redsqua' would be fine though as would be 'res'.
Thus you cannot just search for color codes and you cannot just enumerate all of the possible prefixes for shapes and colors in the same list and use a {0,2} construct to permit 0, 1 or 2 of them in a row. You have to enumerate all of the non-conflicting combinations of prefixes.
You might need to diagram out a transition table like a DFA (deterministic finite automata) and convert it to a long regular expression with lots of "or"
  3 comentarios
Benjamin Deuell
Benjamin Deuell el 7 de Oct. de 2022
Model Analyst, do you have an example of your solution using plot.m to parse LineSpec? in particular I'm wondering if there is a clean way to do this without disrupting other plots I am working with.
Walter Roberson
Walter Roberson el 7 de Oct. de 2022
if you have h = plot(....) then
% cell array. Each entry will be an rgb triple row vector or else 'none'
linecolors = {h.Color}
% cell array of character vectors possibly including 'none'.
markershapes = {h.Marker};
% cell array. Each entry will be an rgb triple row vector or else 'none' or
% 'auto'. typically 'auto'
markeredgecolors = {h.MarkerEdgeColor};
% cell array. Each entry will be an rgb triple row vector or else 'none' or
% 'auto'. Typically 'none'
markerfacecolors = {h.MarkerFaceColor};

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by