MATLAB中的自动重启

MATLAB中的自动重启,matlab,Matlab,MATLAB中是否有命令自动重新启动它(通过“退出”关闭后),并使用不同的变量集运行相同的脚本? 例如,如果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 您可以首先打开matlab的新会话——使用system() 然后您可

MATLAB中是否有命令自动重新启动它(通过“退出”关闭后),并使用不同的变量集运行相同的脚本? 例如,如果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

您可以首先打开matlab的新会话——使用system()
然后您可以在原始会话中调用quit

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

function simpleSum(a, b)

% Use defaults when called without arguments
if nargin < 2
  a = 1;
  b = 2;
end
确保在Matlab调用后面添加一个“&”,以便在后台启动。使用“-r”可以指定在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
函数simpleSum(a,b) %在没有参数的情况下调用时使用默认值 如果nargin<2 a=1; b=2; 结束 fprintf('使用a=%d和b=%d运行simpleSum。\n',a,b); 暂停(5); c=a+b;%在这里实现您的代码 a=b; b=c; %为下一次迭代计算“a”和“b” cmd=sprintf('simpleSum(%d,%d)',a,b);%汇编函数调用 系统(['matlab-r''cmd'&']);%启动新的Matlab实例 退出;%退出当前会话
只需在Matlab命令窗口中使用“simpleSum”即可启动它,它应该反复重新启动Matlab,直到用Ctrl+C手动停止它。

为什么要这样做,而不仅仅是?如果计算运行几天,Matlab响应会因其固有的内存问题而变得非常弱(就目前的问题而言,这个问题仍未解决)。这将需要手动重新启动。目标是退出并使用一组新变量自动重新启动。可能使用bash脚本或类似的工具来运行代码?@David:你能给出一个使用simpleSum.m的示例吗?MATLAB将等待
系统()
命令完成,即等待新的MATLAB会话关闭。在此之前,您不能退出旧会话。它不在循环中工作:对于i=5:6 cmd=sprintf('simpleSum(%d,%d'),i,2);系统(['MATLAB-r“'cmd'&'));退出;结束它关闭初始化实例;然后打开一个新实例并运行第一个值(i=5)并停留在那里-没有进一步的事情发生。你不需要一个循环,因为函数应该在被处理后打开一个新的Matlab实例。它就像一个递归一样工作。请参阅我编辑的文章以获得一个完整的工作示例。
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