G0661 : Input 1 must be a string - error. How do i fix it?

Hello, I am currently trying to use the plotSave command in a loop and dynamically adjust the file name, so to speak. Unfortunately, I always get the error mentioned above. How do I fix this?

This is my code:

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

    _shock = _all_shocks[j];

    plotOpenWindow();

    te=seqa(0,1,TAX.nirf);

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

        j_str = sprintf("%d", j);
        s = "file_" $~ j_str $~ ".pdf";
        fname = strjoin(s, ",");
        plotSave(fname, 7.5|5.5, "in", 150);

    endfor;
endfor;

1 Answer



0



First let's start by explaining and simplifying the code that creates the file name:

// Convert the loop counter integer into a string
j_str = sprintf("%d", j);

// Create a 1x3 string array with the
// desired components of the file name
s = "file_" $~ j_str $~ ".pdf";

// Combine the 3 elements in 's'
// in a 1x1 string array with a comma
// between each of the 3 elements of 's'
fname = strjoin(s, ",");

We can make this much simpler by using the string combine operator like this:

fname = "file_" $+ ntos(j) $+ ".pdf";

The string combine operator $+ will add the elements on either side into a single string. This should solve your problem.

EXTRA DETAILS
I think the above solution is best for this problem, however, for a deeper understanding here are more details.

Internally a GAUSS string and a GAUSS string array are different types. This should usually not be something the user needs to be aware of. However, in this case, plotSave is requiring a string and will not take a string array.

The simplest way to convert a 1x1 string array into a string is by indexing. For example:

// Create a string
s = "Apple";

// Convert it to a string array
s = convertstrtosa(s);

// Convert it back to a string
s = s[1];

aptech

1,773

Your Answer

1 Answer

0

First let's start by explaining and simplifying the code that creates the file name:

// Convert the loop counter integer into a string
j_str = sprintf("%d", j);

// Create a 1x3 string array with the
// desired components of the file name
s = "file_" $~ j_str $~ ".pdf";

// Combine the 3 elements in 's'
// in a 1x1 string array with a comma
// between each of the 3 elements of 's'
fname = strjoin(s, ",");

We can make this much simpler by using the string combine operator like this:

fname = "file_" $+ ntos(j) $+ ".pdf";

The string combine operator $+ will add the elements on either side into a single string. This should solve your problem.

EXTRA DETAILS I think the above solution is best for this problem, however, for a deeper understanding here are more details.

Internally a GAUSS string and a GAUSS string array are different types. This should usually not be something the user needs to be aware of. However, in this case, plotSave is requiring a string and will not take a string array.

The simplest way to convert a 1x1 string array into a string is by indexing. For example:

// Create a string
s = "Apple";

// Convert it to a string array
s = convertstrtosa(s);

// Convert it back to a string
s = s[1];


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.