Message Boards Message Boards

1
|
3840 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Dynamically create a list of strings for chart labels?

Posted 5 years ago

I am attempting to dynamically create a list that will serve as labels for a chart. The list will be based on a name which the user inputs for a list and the range of elements from the list. I can't seem to get the While loop to work just right. What am I doing wrong? What I have so far is:

seq1 = {52.000, 52.289, 39.000, 
   0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615,
    0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
   2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
   74.958, 58.000, 54.000, 68.563, 40.000};
window = 10;
offsetwindow = 5;
wordseqname = "Lab1";
stopper = Length[Partition[seq1, window, window]]; st = 0; While[
 st <= stopper, 
 outlabel = 
  wordseqname <> "_(" <> st*window + 1 <> "-" <> st*window + 1 + 
   window; st++]

The output needs to be:

outlabel={"Lab1 (1-10)","Lab1 (11-20)","Lab1 (21-30)"}
POSTED BY: Jamie Dixson
4 Replies
Posted 5 years ago

Jamie,

I showed a solution to something similar in one of your previous questions. By adjusting the limits and step size passed to Table you can generate whatever pattern and count of labels you need.

wordseqname = "Lab1";
window = 10;
Table[wordseqname <> " (" <> ToString[x] <> "-" <> 
  ToString[x + window - 1] <> ")", {x, 1, 2*window + 1, window}]

(* {"Lab1 (1-10)", "Lab1 (11-20)", "Lab1 (21-30)"} *)

window = 5;

(* {"Lab1 (1-5)", "Lab1 (6-10)", "Lab1 (11-15)"} *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you Rohit for your help. What you have shown almost works right when only the window size is changed but the length of seq1 can also change.

If only the window size changed and seq1 still had 30 elements, then the output should be:

{"Lab1 (1-5)", "Lab1 (6-10)", "Lab1 (11-15)","Lab1 (16-20)", "Lab1 (1-25)", "Lab1 (26-30)"}}

However, the length of seq1 can also change. So, if seq1 was:

seq1 = {52.000, 52.289, 39.000, 
  0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615, 
  0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
  2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
  74.958, 58.000, 54.000, 68.563, 40.000,-3.000, 4.000, 20.378, 20.000, 47.000, 
        74.958, 58.000, 54.000, 68.563, 40.000}

and the window was:

window = 5;

Then the output should be:

{"Lab1 (1-5)", "Lab1 (6-10)", "Lab1 (11-15)","Lab1 (16-20)", "Lab1 (1-25)", "Lab1 (26-30)","Lab1 (31-35)""Lab1 (36-40)"}}

Also, since the elements of the output will be used as labels on a Bar Chart, I need to figure out how to have the quotation marks to be part of the strings as shown.

POSTED BY: Jamie Dixson
Posted 5 years ago

I figured out how to change the range based on the length of seq1 but I am now working on figuring out how to include the quotation marks as part of the string. The solution that I came up with for the length is:

wordseqname = "Lab1";
window = 5;
Table[wordseqname <> " (" <> ToString[x] <> "-" <> 
  ToString[x + window - 1] <> ")", {x, 1, Length[seq1] - window + 1, 
  window}]
POSTED BY: Jamie Dixson
Posted 5 years ago

Rohit, I figure the quotation mark problem out too. Thank you for your help.

POSTED BY: Jamie Dixson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract