Problems plotting specific plots

Hello I have the following, latest problem. I want to create plots and use this code for it:

T.wstar=One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten;

_all_shocks={1,2};
Names=MakeStrMat(10,1);
Names[1]="One";
Names[2]="Two";
Names[3]="Three";
Names[4]="Four";
Names[5]="Five";
Names[6]="Six";
Names[7]="Seven";
Names[8]="Eight";
Names[9]="Nine";
Names[10]="Ten";

Legend=MakeStrMat(2,1);
Legend[1]="Var";
Legend[2]="Cons";

struct plotControl myPlot;
myPlot=plotGetDefaults("XY");
location = "bottom right";
plotSetLegend(&myPlot,Legend,location);
plotSetLegendBorder(&myPlot, "light gray", 1);
plotSetYLabel(&myPlot, "Pct");
plotSetXLabel(&myPlot, "Period");

for j (1, rows(_all_shocks), 1);

  _shock = _all_shocks[j];
  plotOpenWindow();
  te=seqa(0,1,T.nirf);

  for i (1,10,1);
    plotlayout(2,5,i);
    plotSetTitle(&myPlot,Names[i]);
    temp=arraytomat(T.irf[_shock,.,i])~arraytomat(T1.irf[_shock,.,i]);
    plotxy(myPlot,te,temp);
  endfor;
endfor;

Unfortunately, it always creates 10 different plots, although I actually only need 8. I can make it so that, for example, only Names[1] to Names[5] and Names [8] to Names[10] are plotted. Throwing out the individual elements (i.e. Names[6] and Names[7| always gives the error "index out of range"). Thanks for the help

1 Answer



0



You are getting 10 plots, because the plotxy command is being called for each iteration of the loop for i (1, 10, 1); which is run 10 times.

Here is an example that might help. Your current code works like this:

// Create a 6x1 string vector
names = "One" $| "Two" $| "Three" $| "Four" $| "Five" $| "Six";

for i(1, 6, 1);
    print names[i];
endfor;

If you change the above code to this:

// Create a 4x1 string vector
names = "One" $| "Two" $| "Three" $| "Four";

for i(1, 6, 1);
    print names[i];
endfor;

You will get an 'index out of range' error when the loop tries to print the 5th element of names, because it does not exist.

If you make the loop run only as many times as names has elements, then it will work no matter how many elements names has, like this:

// Create a 4x1 string vector
names = "One" $| "Two" $| "Three" $| "Four";

len = rows(names);

for i(1, len, 1);
    print names[i];
endfor;

aptech

1,773

Your Answer

1 Answer

0

You are getting 10 plots, because the plotxy command is being called for each iteration of the loop for i (1, 10, 1); which is run 10 times.

Here is an example that might help. Your current code works like this:

// Create a 6x1 string vector
names = "One" $| "Two" $| "Three" $| "Four" $| "Five" $| "Six";

for i(1, 6, 1);
    print names[i];
endfor;

If you change the above code to this:

// Create a 4x1 string vector
names = "One" $| "Two" $| "Three" $| "Four";

for i(1, 6, 1);
    print names[i];
endfor;

You will get an 'index out of range' error when the loop tries to print the 5th element of names, because it does not exist.

If you make the loop run only as many times as names has elements, then it will work no matter how many elements names has, like this:

// Create a 4x1 string vector
names = "One" $| "Two" $| "Three" $| "Four";

len = rows(names);

for i(1, len, 1);
    print names[i];
endfor;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.