run matlab script from linux shell with change aregument

16 visualizaciones (últimos 30 días)
abbas hasan
abbas hasan el 21 de Jul. de 2014
Respondida: Shivam el 24 de Jun. de 2023
hello, do you have idea how to run Matlab script from linux shell with change parameter or argument in matlab script. i do this for fixed variable after write linux script : matlab -nodisplay -nodesktop -nosplash -r "run directory/matlab script name.m; quit" it's work fine , but i dont have idea how make this work with change variable in matlab script with for loop.
many thanks to any help.
abbas,

Respuestas (1)

Shivam
Shivam el 24 de Jun. de 2023
Hi Abbas,
To run a MATLAB script with changing arguments from a shell script, you can pass the argument variable names to the MATLAB script using the -r flag. Inside the MATLAB script, prompt for the argument variable name, retrieve its value using evalin, and perform the desired operations. Here are the steps:
1) Create a MATLAB script, e.g., test_script.m, that accepts a variable name as input, retrieves its value using evalin, and performs operations based on the value.
% test_script.m
% Accept command-line argument variable name
argName = input('Argument variable name: ', 's');
% Get the value of the argument variable
argValue = evalin('caller', argName);
% Display the argument
disp(['Argument: ' num2str(argValue)]);
2) Write a shell script, e.g., run_test_script.sh, that iterates over an array of argument variable names. Invoke MATLAB with each argument variable name using the -r flag and pass the argument name to the MATLAB script.
#!/bin/bash
# Array of argument variable names
arguments=("arg1" "arg2" "arg3")
# Iterate over the array
for arg in "${arguments[@]}"; do
# Run MATLAB script with argument
matlab -nodisplay -nodesktop -nosplash -r "argName='$arg'; run('test_script.m'); exit"
done
3) Set permissions for the shell script using chmod +x run_test_script.sh.
4) Run the shell script using ./run_test_script.sh to execute the MATLAB script with varying argument values.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by