Matrix as argument for user-defined function

Hello,

I am trying to pass a matrix into a user-defined function, which is also suppposed to return a matrix.

Upon syntax vertification, this is being rejected.

Is there any way to accomplish this?

Thanks

Robert

Can you post the code that you have written along with AB version.

@schling,

You need to post example!

Please re-read "How to ask good question..."

https://forum.amibroker.com/t/how-to-ask-a-good-question/10247

"
Upon syntax vertification... "

You have not posted exact error message in addition.

But you should do so instead of just mentioning "...returns error and similar...".

Readers can not see your screen. There are multiple different type of errors that can happen.

There is zero issue of matrix arguments in custom functions.

So guessing games...

Apparently you are trying to pass matrix being stored to static variables and if static var is empty then it would return type number which would result in Error 5 -> incorrect argument type.

ยดยดยด

if ( typeof(mat) == "matrix" ) {

// then do matrix stuff

} else {

// do other handling

}

ยดยดยด

2 Likes

Sorry about the sloppy post guys ...

As I could not find an answer in the documentation or in this forum, I concluded -wrongly- that matrices are not allowed as arguments.

Thanks for saying that it works. Believe it or not, when I tried to put together a simplified version of the code to demonstrate the problem, the problem went away.

Not a great start on this forum :slight_smile:

1 Like

When asking questions please always include the formula you are trying. As others already said, passing and returning matrices works fine, example:

// this function works on matrices, arrays and numbers
function Twice( input )
{
   return 2 * input;
}

mx = MxFromString( "{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }" );

printf("input matrix %s\n", MxToString( mx ) );

result = Twice( mx );

printf("output matrix %s\n", MxToString( result ) );

Produces this output:

input matrix {
 { 1, 2, 3 },
 { 4, 5, 6 },
 { 7, 8, 9 }
}
output matrix {
 { 2, 4, 6 },
 { 8, 10, 12 },
 { 14, 16, 18 }
}
2 Likes

Thanks Tomasz, I am all set now!