如何从MATLAB在bash脚本的返回值? [英] How to get the return value from matlab in bash script?

查看:326
本文介绍了如何从MATLAB在bash脚本的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个matlab函数

If I have this matlab function

function [result] = matlab_test(param1, param2)

disp(sprintf('param1 : %s', param1));
disp(sprintf('param2 : %s', param2));

result = 'hello matlab';

和我想打电话给在bash脚本此matlab函数像

And I want to call this matlab function in bash script like

matlab -nodesktop -nosplash -nodisplay -r "try, A=matlab_test('test','matlab'); end; quit"
echo $A

和我想这个输出

test
matlab
hello matlab

这里我的要求是找到,如果它可能使用在我bash脚本从Matlab的返回值,并通过管道向它传递。我只想让我的应用程序分割成小的组件(文件),并通过管道/ PARAMS通信。总之,我想看看我到底能换Matlab的脚本在我的bash脚本,这样我可以把我的code结构。结果
如果我不能做到这一点,我会简单地捆绑在一起,我需要从MATLAB的返回值一起到MATLAB脚本中的一部分。

My requirement here is to find if it is possible to use the return value from Matlab in my bash script and also to pass it through the pipeline. I only want to make my application separated into small components (files) and communicate through pipeline/params. In short, I want to see how far I can wrap Matlab script in my bash script so that I can set my code architecture.
If I cannot do this, I'll simply bundle the part that I need the return value from matlab together into matlab script.

推荐答案

您会问两个问题。我会回答这两个,包括为什么第二个可能是根据您的操作系统上是不可能的(和我的肯定是不可能的),然后提供一个问题解决方法相关的建议。

You're asking two questions. I'll answer both, including why the second one might be impossible depending on your operating system (and definitely impossible on mine), then offer a suggestion on a problem dependent workaround.

首先,我越来越Matlab的同一个外壳交互时使用这样的脚本。

First, I use a script like this when getting Matlab to interact with a shell.

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay 
A=matlab_test('$1','$2');
system(['export temp1=' A]); %doesn't work
setenv('temp2',A); %also doesn't work, I'll explain why below
exit
EOF
echo $temp1
echo $temp2

给出输出:

[XXXXXX@compute-0-138 ~]$ ./stack_ex test matlab
Warning: No window system found.  Java option 'MWT' ignored

                            < M A T L A B (R) >
                  Copyright 1984-2010 The MathWorks, Inc.
                Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
                               March 18, 2011


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

>> param1 : test
param2 : matlab
>> >> >> 

所以很明显设置环境变量的两个版本无法正常工作。这使我们您的第二个问题。

So clearly the two versions of setting environment variables doesn't work. This leads us to your second question.

未能回响背后的原因是,无论系统 SETENV 创建关闭时弹Matlab的关闭。也就是说,MATLAB不能设置调用它的外壳外面的环境变量。

The reason behind the failure to 'echo' is that both system and setenv create shells that are closed when Matlab is closed. That is to say, Matlab cannot set environment variables outside the shell that called it.

有对这个解决办法在此张贴讨论 Windows系统上,使用微软的工具。还提到<一个href=\"http://www.mathworks.com/support/solutions/en/data/1-3IOL0N/index.html?product=SL&solution=1-3IOL0N\">here.

There's a workaround for this for Windows systems discussed in this posting, that uses a tool from Microsoft. Also mentioned here.

我不相信有针对* nix系统的解决方法,从内部Matlab的设置环境变量。

I don't believe there's a workaround for *nix systems to set environment variables from within Matlab.

下面是做同样的事情到你所描述的方法。

Here's a method to do something similar to what you described.

我假设使用echo是不是你真正想做的事。相反,我猜你想使用存储在环境变量与在shell命令或脚本的进一步工作中使用的字符串输出。一个可能的解决方法是以下内容:

I'm assuming the use of echo is not what you actually want to do. Rather, I'm guessing that you'd like to use the string output stored in the environment variable to be used in further work with commands or scripts in the shell. One possible workaround would be the following:

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay 
A=matlab_test('$1','$2');
setenv('temp1',A); %doesn't work
[a b] = system(['echo ' '$' 'temp1'])
exit
EOF

捐赠输出:
[XXXXXX @计算-0-138〜] $ ./stack_ex_3测试MATLAB
警告:未找到窗口系统。 java选项MWT忽略

Giving output: [XXXXXX@compute-0-138 ~]$ ./stack_ex_3 test matlab Warning: No window system found. Java option 'MWT' ignored

                            < M A T L A B (R) >
                  Copyright 1984-2010 The MathWorks, Inc.
                Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
                               March 18, 2011


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

>> param1 : test
param2 : matlab
>> >> 
a =

     0


b =

hello matlab

这表明命令回声$ temp1目录 SETENV 创建子shell和temp1中进行了评估持有的价值分配给它。此调用壳的结果现在存储在B(A持有0表示成功)。可以想象的是,你可以通过整个的你想在通过系统命令壳里做什么,所以它在子shell中运行。我们必须知道你的问题的更多的细节给这个方法的全面评估,但。

This shows that the command echo $temp1 was evaluated in the subshell created by setenv and temp1 holds the value assigned to it. The result of this call to the shell is now stored in b (a holds 0 indicating success). It is conceivable that you could pass the whole of what you would like to do in the shell through the system command, so that it runs in the subshell. We would have to know more specifics of your problem to give a complete assessment of this approach though.

编辑和后续的 * 的**的 * 的**的 * 的**的 *

Edits and followup **********

最接近包装Matlab和庆典,我能想到的是下面的技巧。可以通过管道从MATLAB脚本输出到 myresult.out 通过以下内容:

The closest thing to wrapping Matlab and bash that I can think of is the following trick. You can pipe the output from the Matlab script to myresult.out with the following:

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay /> myresult.out 
A=matlab_test('$1','$2');
disp(['grepMe ' A])
exit
EOF

您可以grep从 grepMe myresult.out ,管sed和选择只把部分你所需要的输出线,然后通过管道上脚本的其余部分。那我认为你可以得到你想要做什么的接近。

You can grep the grepMe line from myresult.out, pipe to sed, and select only the part of the output line you need, then pipe that on the the rest of your script. That's as close as I think you can get to what you're trying to do.

这篇关于如何从MATLAB在bash脚本的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆