5
$\begingroup$

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"
$\endgroup$
2

1 Answer 1

4
$\begingroup$

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"

$\endgroup$
4
  • $\begingroup$ 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, ... $\endgroup$
    – DPF
    Nov 9, 2016 at 15:34
  • $\begingroup$ 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. $\endgroup$
    – corey979
    Nov 9, 2016 at 15:41
  • $\begingroup$ Well at least now, i start hearing it. Ok, I'll get rid of the subscripts. Would you mind adding , ":" -> "", "." -> "" to your StringReplace? $\endgroup$
    – DPF
    Nov 9, 2016 at 15:45
  • $\begingroup$ Now the answer is complete and works with the messy strings as well. $\endgroup$
    – corey979
    Nov 9, 2016 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.