5

When I extract PlotLabel from my List of plots using

PlotLabel /. AbsoluteOptions[plotList[[1]], PlotLabel]

I get strings like

"M vs. \!\(\*SubscriptBox[\(n\), \(sync\)]\) - Dutycycle: 20%"
  (* or *)
"M vs. Subscript[n, sync] - Dutycycle: 20%"

rendered string

Now I want to export this list of plots in such a way, that the file-name is related to the PlotLabel that I extracted before.

How can I make this string suitable for a filename, including:

  • replace white-space with "_"
  • make subscript just normal: Subscript[n, sync] --> nsync
  • remove characters that cannot be in file-names, like ":" or "%"

?

A result could look like

"M_vs_nsync_-_Dutycycle_20"
corey979
  • 23,947
  • 7
  • 58
  • 101
DPF
  • 1,067
  • 8
  • 19

1 Answers1

4

EDIT:

With the more messy form of the string:

str = "M vs. \!\(\*SubscriptBox[\(n\), \(sync\)]\) - Dutycycle: 20%"

one can use the function deSubscript by glS (thanks to Mr.Wizard):

deSubscript[string_] := StringReplace[string,
  "\!\(\*SubscriptBox[\(" ~~ Shortest[x__] ~~ "\), \(" ~~ 
    Shortest[y__] ~~ "\)]\)" :> x <> y
  ]

to get

str1 = deSubscript[str]

"M vs. nsync - Dutycycle: 20%"

and then proceed with

StringReplace[str1, {" " -> "_", "%" -> "", "." -> "", ":" -> ""}]

"M_vs_nsync_-_Dutycycle_20"

corey979
  • 23,947
  • 7
  • 58
  • 101
  • looks good, but see my edited question: I seem to actually get another string than I pasted here before: "M vs. \!\(\*SubscriptBox[\(n\), \(sync\)]\) - Dutycycle: 20%". I'm a little lost in InputForm, FullForm, ... – DPF Nov 09 '16 at 15:34
  • And that's another reason why you'll hear here a mantra "don't use subscripts" - when you want to work with them further, they mess things up. – corey979 Nov 09 '16 at 15:41
  • Well at least now, i start hearing it. Ok, I'll get rid of the subscripts. Would you mind adding , ":" -> "", "." -> "" to your StringReplace? – DPF Nov 09 '16 at 15:45
  • Now the answer is complete and works with the messy strings as well. – corey979 Nov 09 '16 at 15:57