首页 > 解决方案 > 在 MATLAB 中,如何将 Unix 标准输出存储在单元格数组中以进行多行命令?

问题描述

我必须在没有 MATLAB 的远程系统上运行 unix 代码。我这样称呼它

% shared key between computers so no password required to type in
ssh_cmd = 'ssh login@ipaddress '; 
for x = 1:nfiles
  cmd = sprintf('calcPosition filename%d',x); 
  % as an example, basically it runs a c++ command 
  % on that computer and I pass in a different filename as an input
  full_cmd = [ssh_cmd, cmd];
  [Status,Message] = system(full_cmd);
  % the stdout is a mix of strings and numbers
  % <code that parses Message and handles errors>
end

对于一个小型测试,这大约需要 30 秒才能运行。如果我这样设置

full_cmd = [ssh_cmd, cmd1; cmd2; cmdN]; % obviously not valid syntax here
% do the same thing as above, but no loop

大约需要 5 秒,所以大部分时间都花在连接到其他系统上。但是Message是所有 n 个文件的组合标准输出。

我知道我可以将输出通过管道传输到单独的文件,但我宁愿将其直接传递回 MATLAB 而无需添加 I/O。那么有没有办法将标准输出(Message)作为每个单独命令的单元格数组(或 Unix 等效项)?然后我可以循环遍历 的单元格,Message并且函数的其余部分不需要更改。

我考虑过在每个命令调用周围使用标识符文本作为帮助解析的一种方式Message,例如

full_cmd = [ssh_cmd; 'echo "begincommand " '; cmd1; 'echo "begincommand " '; cmd2]; % etc

但一定有比这更优雅的东西。有小费吗?

标签: bashmatlabunixsystemstdout

解决方案


推荐阅读