run matlab script from linux shell with change aregument
조회 수: 23 (최근 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에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!