Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the closest way to pass string arguments from bash script to matlab file?

I have matlab file matlab_param.m

function matlab_param(param1, param2)

disp(sprintf('param1 : %s', param1));
disp(sprintf('param2 : %s', param2));

And I want to have bash script bash_param.sh that look like

#!/bin/bash
echo $1
echo $2
./matlab_param.m $1 $2

I want to run this bashscirpt

./bash_param.sh hello world

and it will print

hello
world
param1 : hello
param2 : world

I googled for hours and couldn't find any exact solution for this. The closest one the I got so far is

matlab -nodesktop -nosplash -nodisplay -r "try, run ('./test_param.m'); end; quit"

which I need to hardcode all parameters.

like image 417
Jessada Thutkawkorapin Avatar asked Apr 05 '12 13:04

Jessada Thutkawkorapin


People also ask

How do you pass an argument to a function in Bash?

To pass any number of arguments to the bash function simply put them right after the function's name, separated by a space. It is a good practice to double-quote the arguments to avoid the misparsing of an argument with spaces in it. The passed parameters are $1 , $2 , $3 …

How do you pass arguments to a shell script?

To invoke a function, simply use the function name as a command. To pass parameters to the function, add space-separated arguments like other commands. The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3, etc.

How many arguments can be passed to bash script?

NUM} ) in the 2^32 range, so there is a hard limit somewhere (might vary on your machine), but Bash is so slow once you get above 2^20 arguments, that you will hit a performance limit well before you hit a hard limit.


2 Answers

Did you try:

#!/bin/bash
echo $1
echo $2
matlab -nodesktop -nosplash -nodisplay -r "try, test_param('$1','$2'); end; quit"
like image 195
Oli Avatar answered Nov 10 '22 13:11

Oli


If you want to be able to pass arguments to matlab function I'd recommend to create a simple shell script:

matlab -nodisplay -r "try, test_param('$1','$2'); end; exit"

Then you can run in unix:

$ sh test_param.sh hello world

Not sure although how to avoid the MATLAB header output and if it will be passed to pipe.

like image 42
yuk Avatar answered Nov 10 '22 11:11

yuk