在MATLAB中自动重启

时间:2015-12-23 00:50:58

标签: matlab

MATLAB中是否有一个命令可以自动重启它(在通过'quit'关闭它之后)并使用一组不同的变量运行相同的脚本? 例如,如果simpleSum.m是代码:

a=1;
b=2;
abSum=a+b
quit
%some command here to restart matlab with (say) a=3 and b=5; 
%and then with a=5, b=-2; Then with a=7, b=-5 and so on

2 个答案:

答案 0 :(得分:0)

你可以先打开matlab的新会话 - 使用system() 然后你可以在原来的会话上打电话退出

答案 1 :(得分:0)

我认为你应该使用函数而不是脚本。然后你可以轻松地将初始参数传递给它:

function simpleSum(a, b)

% Use defaults when called without arguments
if nargin < 2
  a = 1;
  b = 2;
end

完成计算并重新启动Matlab后,您可以执行以下操作:

% Calculate 'a' and 'b' for the next iteration
cmd = sprintf('simpleSum(%d, %d)', a, b);  % Assemble function call
system(['matlab -r "' cmd '"&']);          % Start new Matlab instance
quit;                                      % Quit current session

确保添加&#39;&amp;&#39;你身后的Matlab调用,这样它就会在后台启动。使用&#34; -r&#34;你可以指定一个在Matlab启动时应该执行的命令。

编辑: 这是我测试的完整文件(simpleSum.m):

function simpleSum(a, b)

% Use defaults when called without arguments
if nargin < 2
  a = 1;
  b = 2;
end

fprintf('Running simpleSum with a=%d and b=%d.\n', a, b);
pause(5);

c = a + b;   % Implement your code here
a = b;
b = c;

% Calculate 'a' and 'b' for the next iteration
cmd = sprintf('simpleSum(%d, %d)', a, b);  % Assemble function call
system(['matlab -r "' cmd '"&']);          % Start new Matlab instance
quit;                                      % Quit current session

只需使用&#34; simpleSum&#34;在Matlab命令窗口启动它,它应该重复重启Matlab,直到用Ctrl + C手动停止。

相关问题