Dynamically form 1st argument name of Plot in a loop | Getting Error 5. Argument #1 has incorrect type

Dear Experts,

Got a function that returns multiple values. Have followed what the KB taught. Now the returned arrays are several in number and are called by their assigned alphanumeric unique ID ("alpha" part is a string constant and "numeric" part can be sequentially incremented).

I do not want to individually call these arrays to Plot because of their sheer number. So, by the help of a For loop I am writing a single Plot for plotting purposes of the arrays. Now here is the catch. Since I am using string constant "alpha" part first, then using the sequentially incremented For loop's counter as the "numeric" part; the Plot() function is unable to recognize the formed dynamic string as a previously assigned Array Name and unable to fetch its Elements which were formed by calling the function to begin with. Thus I get Error 5 :rofl:

Any solution?

Below is a code snippet that throws the Error 5:

function FunkyFunc( GotOthrArr1, GotOthrArr2, GotOthrArr3, GotNewArr1, GotNewArr2, GotNewArr3, GotNewArr4, GotNewArr5 )
{
	 //Calculations and using VarSet/varGet to return NewArr1 to 5
	 //BTW in real case there are many more arrays that called and returned
}

NewArr1 = 0;
NewArr2 = 0;
NewArr3 = 0;
NewArr4 = 0;
NewArr5 = 0;
FunkyFunc( "OtherArr1", "OtherArr2", "OtherArr3", "NewArr1", "NewArr2", "NewArr3", "NewArr4", "NewArr5" );

CmrTmfTxt = "New";
LvlCntDec = 5;
for( j = i + 1; j <= i + 5; j++ )
{
	 if( j <= ( i + 5 ) )
	 {
		 arr = CmrTmfTxt + "Arr" + LvlCntDec;
		 Plot( IIf( ConditionMet[ j ], arr, Null ), "", colorWhite, styleLine| styleNoRescale | styleNoLabel );
		 LvlCntDec--;
	 }
}

Thank you

@cougar I suggest you to use a different approach: pass the parameters by reference to your function.

See for instance this past thread.

This feature was introduced in version 6.25 Beta (and successively improved):

  • AFL: New feature: Passing arguments as reference (allows modification of arguments passed – easy way to return multiple values), to pass by reference use & (address-of) operator before variable identifier)

I use it frequently, as I did in this "sorts" porting code example.

The most recent beta versions should work without any issues.

1 Like

Sorry @beppe I am unable to comprehend, just going in circles.

@cougar, my previous suggestion was only related to the most efficient way to return multiple values from a function (especiallye when dealing with arrays). Sorry for not making it more clear.

Re your code, you probably can achieve what you want simply using VarSet() / VarGet() functions doing something similar to this snippet (modeled as per your example; I assigned some values to the arrays so it can be executed - obviously it has no practical meaning).

maxArray = 5;
for (i = 1; i <= maxArray; i++)
	VarSet("NewArr" + i, C);

function FunkyFunc( multiplier )
{
	for (i = 1; i <= maxArray; i++)
	{
		a = VarGet("NewArr" + i);
		a = a * (multiplier * i);
		VarSet("NewArr" + i, a);
	}	
	
}
multiplier = 2;

FunkyFunc( multiplier );

ConditionMet = C > MA(C, 200);
for (i = 1; i <= maxArray; i++)
{
	arr = VarGet("NewArr" + i);
	Plot( IIf(conditionMet, arr, Null), "NewArr" + i, 2 + i, styleLine| styleNoLabel );
}
2 Likes