分析从MATLAB到Linux,反之亦然变量在shell [英] parsing variables from MATLAB to Linux and vice versa in a shell

查看:289
本文介绍了分析从MATLAB到Linux,反之亦然变量在shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也遇到了问题。我必须使用MATLAB与Linux操作系统。
我需要从MATLAB数据解析到Linux,反之亦然。

I have encountered a problem. I have to use MATLAB with linux. I need to parse data from MATLAB to Linux and vice versa.

有关exammple

For exammple

这一切都在writtein

This all is writtein in

basic.sh

this basic.sh has to be opened in MATLAB

 s=3     # is defined is MATLAB
##########################


for (( p=1 ; p<5; p++ ))     # from here starts the loop in Linux
do                                # is a command from Linux
echo "$p"                         # is a command from Linux
add= $p+s                         # should calulate in linux , is a command from Linux
add=add/5                         # should do in MATLAB 
done     

#########################
 add                              # should OUTPUT the value of add as there is no semicolumn in MATLAB 

请建议我这样一个小例子,其余的我将扩大它自己一个可行的办法。

Kindly suggest me a possible way for such a small example the rest I will expand it myself.

问候

推荐答案

那么,你可以从终端调用Matlab的,并运行一个命令:

Well, you can call Matlab from a terminal, and run a single command:

$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND>

&LT; YOUR_COMMAND&GT; 可以是M-脚本/功能。这个输出可以被重定向到shellscripts,

in which <YOUR_COMMAND> can be an m-script/function. The output of this can be redirected into shellscripts,

$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND> | ./basic.sh

(你的脚本应该能够处理管道),或者这整个命令可以嵌入shell脚本,

(your script should be capable of handling pipes), or this entire command can be embedded in shell scripts,

#!/bin/bash

s=$(matlab -nodesktop -nojvm -nosplash -r <FUNCTION_GENERATING_S>)

<code generating $add>

result=$(matlab -nodesktop -nojvm -nosplash -r <SOME_FUNCTION($add)>)

您当然也可以诉诸使用文件作为内存。 Matlab的一部分:

You can of course also resort to using files as memory. Matlab part:

s=3;      

fid = fopen('TMP.txt','w');
fprintf(fid, s);
fclose(fid);

!./basic.sh

fid = fopen('TMP.txt','r');
add = fscanf(fid, '%f');
fclose(fid);

shell脚本:

Shell script:

#!/bin/bash

s=$(cat TMP.txt)
for (( p=1; p<5; p++ ))     
do                            
    echo "$p" 
    add=$(($p+$s))
    add=add/5                      
done

echo $add > TMP.txt

这个好处是,只有一个m文件就足够了Matlab和shell脚本之间严格的分离,。

Advantage of this is that there is a strict separation between Matlab and shell script, and only one m-file will suffice.

当然,无论您选择何种方式 - 你为什么会想这样做摆在首位? MATLAB可以做什么大部分的bash能够,也是独立于平台的(所以如果你切换到MS Windows中,这一切仍然有效)...所以你能澄清这一点?

Of course, whichever way you choose -- why would you want to do this in the first place? Matlab can do most of what bash is capable of, and is also platform independent (so if you switch to MS Windows, it all still works)...so can you clarify this a bit?

这篇关于分析从MATLAB到Linux,反之亦然变量在shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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