How can I run a MATLAB function using Windows Command Prompt and providing input for the function?

Hello,
I am trying to execute an .m file containing a function using the Windows command prompt. The function has 3 inputs, how can I execute the file while providing the data for the inputs using the command prompt?
Thank you,

回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 9 月 4 日

8 件のコメント

mPayns
mPayns 2018 年 9 月 4 日
Hello, I already checked the link and I was not able to find any information on how to provide the input information. I can execute the file using the command prompt without a problem but I can't provide the input arguments of the function.
For example if my function looks like this: function [ output_args ] = myfunction(x1, x2, x3 ) is there a way to provide the data for x1, x2 and x3 and execute the file using the command prompt?
You're starting MATLAB with the -r startup option listed here?
matlab -r "y = plus(1, 2)"
This will start a fresh MATLAB session and execute the command y = plus(1, 2).
athi123
athi123 2019 年 7 月 18 日
編集済み: athi123 2019 年 7 月 18 日
Hi, I am wondering if it would be possible to pass in a variable as an argument. For example, is there a way to do something sort of like this:
x = 2
z = 23
matlab -r "y = plus(x, z)"
?
edit: For context, I'm not trying to do this in the command prompt, I'm trying to do this inside a script so it'd be something like:
x = 2
z = 23
!matlab -r "y = plus(x, z)"
Given what you wrote in your edit, the bang operator (!) is the wrong tool to use. Build the command you want to execute and pass it into the system function. For example, since x and z are integer values:
x = 2;
z = 23;
commandToRun = sprintf('matlab -r "y = plus(%d, %d)"', x, z)
system(commandToRun)
Though if you're trying to run a non-interactive command and you're using release R2019a or later, prefer using the -batch startup option.
athi123 could you clarify if you want to do this in a matlab script to start an external command, or if you want to do this from a .bat file or Unix shell script?
athi123
athi123 2019 年 7 月 19 日
Yes sorry, matlab script
I think what Steven told me should work, thanks!
Mitchell Herndon
Mitchell Herndon 2019 年 10 月 24 日
編集済み: Mitchell Herndon 2019 年 10 月 24 日
How could you format the -r statement to include a try/catch?
Such as
matlab -r "try (code) catch ME (code) end"
matlab -r "try (code); catch ME; (code); end"

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2017a

質問済み:

2018 年 9 月 4 日

コメント済み:

2019 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by