如何从Python执行MATLAB函数并将参数传递给它?
我的测试MATLAB函数文件称为Hello_world.m,其代码是:
function [printed_line] = hello_world(name)
str_to_display = strcat('Hello World from: ', string(name))
disp(str_to_display)
printed_line = strlength(name)
return printed_line
我目前正在尝试使用以下行调用它:
import subprocess
my_name = 'john wayne'
run_matlab = 'matlab -nodisplay -nojvm -nosplash -nodesktop -r "try, hello_world my_name, catch e, disp(getReport(e)), exit(1), end, exit(0);"'
subprocess.run(run_matlab, shell=True)
我想:
- 结果为
Hello World来自:John Wayne
而不是Hello World of:my_name
- 获取功能的返回变量
- 可能避免加载MATLAB的加载,
而MATLAB当然会调用该命令时,我想调用更复杂的功能,这只是一个最小的可重复性例子。
My test matlab function file is called hello_world.m and its code is:
function [printed_line] = hello_world(name)
str_to_display = strcat('Hello World from: ', string(name))
disp(str_to_display)
printed_line = strlength(name)
return printed_line
I am currently trying to call it using the following lines:
import subprocess
my_name = 'john wayne'
run_matlab = 'matlab -nodisplay -nojvm -nosplash -nodesktop -r "try, hello_world my_name, catch e, disp(getReport(e)), exit(1), end, exit(0);"'
subprocess.run(run_matlab, shell=True)
I would like to:
- the result to be
Hello World from: john wayne
instead ofHello world from: my_name
- get the returned variable of the function
- possibly avoid the loading of matlab that happens when calling that command
Of course, I would like to call more complex functions, this is just a minimal reproducible example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论