run matlab script from linux shell with change aregument
16 次查看(过去 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,
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!