run matlab script from linux shell with change aregument

22 次查看(过去 30 天)
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,

回答(1 个)

Shivam
Shivam 2023-6-24
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by