如何从Python执行MATLAB函数并将参数传递给它?

发布于 2025-02-10 23:37:56 字数 820 浏览 1 评论 0原文

我的测试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)

我想:

  1. 结果为Hello World来自:John Wayne而不是Hello World of:my_name
  2. 获取功能的返回变量
  3. 可能避免加载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:

  1. the result to be Hello World from: john wayne instead of Hello world from: my_name
  2. get the returned variable of the function
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文