The first two parameters of the script are passed in reverse order when running with compiled Brainstorm

I found something weird when debugging.
The part of my script with parameters is :

The command I run is :

The results of 'disp' is:

So, the first two parameters were passed in reverse while the third and the fourth were in normal order. That really puzzles me!

The first thing I can notice from your command line call is that you are using single quotes ' to define your arguments. To pass string arguments from the command-line, you should probably be using double quotes instead, or even less confusing for the parser: no quotes at all.
If you have special characters in the paths (like spaces), escape them with a blackslash \ instead.
This might be causing trouble in the interpretation of the arguments.

Note that you can also test your script from Matlab before trying to deploy it with the Matlab Runtime.
brainstorm script arguments works also from the Matlab command line.
If it works from Matlab and not from the terminal, it indicates that your command line call has an issue.

To explore this further, I'd recommend you start with a simpler script, passing only simple values instead of full paths. Example script:

function test(arg1, arg2, arg3, arg4)
disp(arg1)
disp(arg2)
disp(arg3)
disp(arg4)

Execution from a Windows CMD:

C:\Work\Dev\brainstorm3\bin\R2022b>brainstorm3 C:\Work\Dev\brainstorm3\test.m 1 2 3 4

Matlab R2022b found:
C:\Program Files\MATLAB\R2022b

Please wait...

If it hangs for more than a few minutes: try pressing ENTER.
Alternatively, download Brainstorm for a different version of the Matlab Runtime.
(See the installation instructions on the Brainstorm website)


BST> Starting Brainstorm:
BST> =================================
BST> BrainstormHomeDir = C:\Users\franc\AppData\Local\Temp\franc\mcrCache9.13\bst_ja0\bst_javabuil
BST> Version: 01-Mar-2023
BST> Deleting old process reports...
BST> Loading configuration file...
BST> Plugin [...]
BST> Reading process folder...
BST> Loading current protocol...
BST> =================================

WARNING: This file is a function, trying to convert to a script...
1
2
3
4
BST> Emptying temporary directory...
BST> Brainstorm stopped.
BST> Compiled code returned without errors.
C:\Work\Dev\brainstorm3\bin\R2022b>

Please let us know if it works without the quotes, I would add a warning in the Scripting tutorial about using quotes from the shell command line.
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Without_Matlab

Thank you for your prompt reply! I will try it and come back with a feedback.

Hi Francois,

I have tested all the points you mentioned above. After a lot of testing, I finally figured out why the args were in reverse :smiling_face_with_tear:.
The reason is that the passing args were sorted.

/data/xiuying/Project/BrainstormTest/bst_with_libraries/brainstorm3/bin/R2022b/brainstorm3.command /usr/local/MATLAB/MATLAB_Runtime/v913 test.m "/data3/" "/data1/" "/data2/" "/data4/" local
/data/xiuying/Project/BrainstormTest/bst_with_libraries/brainstorm3/bin/R2022b/brainstorm3.command /usr/local/MATLAB/MATLAB_Runtime/v913 test.m "/data4/" "/data1/" "/data2/" "/data3/" local

As you can see, the args would be sorted. So, please help to see how to solve it.

Thanks

Nothing is expected to sort the parameters.
I've tested on Linux too, with the script I shared previously:

$ /mnt/c/Work/Dev/brainstorm3/bin/R2022b/brainstorm3.command /home/ftadel/MATLAB_R2022b/ /mnt/c/Work/Dev/brainstorm3/test.m 4 3
 2 1

Please wait...

If it hangs for more than a few minutes: try pressing ENTER.
Alternatively, download Brainstorm for a different version of the Matlab Runtime.
(See the installation instructions on the Brainstorm website)


BST> Starting Brainstorm:
BST> =================================
BST> BrainstormHomeDir = /home/ftadel/.mcrCache9.13/bst_ja0/bst_javabuil
BST> Version: 01-Mar-2023
BST> Deleting old process reports...
BST> Loading configuration file...
BST> Plugin brain2mesh: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/brain2mesh
BST> Plugin iso2mesh: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/iso2mesh
BST> Plugin brainentropy: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/brainentropy
BST> Plugin blackrock: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/blackrock
BST> Plugin neuroelectrics: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/neuroelectrics
BST> Plugin xdf: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/xdf
BST> Plugin libsvm: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/libsvm
BST> Plugin fastica: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/fastica
BST> Plugin picard: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/picard
BST> Plugin derivelfp: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/derivelfp
BST> Plugin nirstorm: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/nirstorm
BST> Plugin mcxlab-cl: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/mcxlab-cl
BST> Plugin mia: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/mia
BST> Plugin spm12: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/spmtrip
BST> Reading process folder...
BST> Loading current protocol...
BST> =================================

WARNING: This file is a function, trying to convert to a script...
4
3
2
1
BST> Emptying temporary directory...
BST> Brainstorm stopped.
BST> Compiled code returned without errors.

I recommend you try to reproduce this very example first.

Thanks, Francois. I've tested again with this very example you shared and recognized the problem.
Nothing wrong happened until the argument 'local' was added at the end.


Is there any way to fix it?

You nailed it!
The use of "setdiff" to remove the "local" parameter from the list of arguments was also SORTING the arguments.
I fixed it in this commit: Bugfix: "brainstorm arg1 .. argN local" was sorting the arguments · brainstorm-tools/brainstorm3@07a5f86 · GitHub
I still need to recompile Brainstorm and update it on the website. Will do this is within a few hours, please wait for a while before updating the compiled version of Brainstorm and trying again.

Thank you for helping us fix this nasty bug!

The fixed compiled version of Brainstorm is now available online on the Download page.

Thank you so much for such a quick response! :smile: :smile: