本文介绍了如何将数据传递给MATLAB oncleanup函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已编译的matlab程序,该程序可以自动调整机器参数.在调整周期结束时,我需要保留一些原始设置.有时会发生意外错误,有时用户会看到调整算法无法正常工作,因此应终止(使用control-C).

I have a compiled matlab program that automatically tunes machine parameters. At the end of a tuning cycle I need to reistate some original settings. Sometime unexpected errors occur and sometimes the user can see that tuning algorithm is not working properly so should be terminated (with control-C).

如果发生可预测的错误,我可以使用try catch块恢复参数.但是,如果发生意外错误或用户调用了Control-C,则程序将退出而不会通过catch子句,并且计算机将处于未定义状态.

If a predictable error occurs, I can reinstate parameters with a try catch block. However, if an unexpected error occurs or the user invokes control-C, the program exits without passing though the catch clause and the machine is left in an undefined state.

我知道我可以注册一个清理函数,该清理函数将在我的工作函数完成时正常运行,这通常是通过可预测或不可预测的错误,或者是Control-C事件.理想情况下,清除功能将不执行任何操作,如果发生可预测的错误,则将控制权传递给顶层进行清除.如果发生Control-C事件或不可预测的错误,清理功能应警告用户该程序失败,以便他们手动清理.

I know I can register a cleanup function that will run as my working functions finish, either normally, via an predictable or unpredictable error, or control-C event. Ideally, the the clean up function would do nothing and pass control to the top level to clean up if a predictable error occurs. If a control-C event or an unpredictable error occurs, clean up function should warn the user that the program failed so they clean up manually.

为此,我需要在运行时使清除函数知道一个预测的或非预测的终止(即control-C)的事实.我知道清理函数在注册时会获取参数值的副本,并且这些值无法在运行时更改,因此传递参数将无法进行.我认为应该起作用的是将清除函数嵌套在我的工作函数中,以便封闭函数中的局部变量可用于清除函数.但是,这不起作用.

To do this, I need to make the fact of a predicted or not-predicted termination (i.e. control-C) known to the clean up function at run time. I know that the cleanup function takes a copy of parameter values when it is registered, and that these cannot be changed at run time, so passing in a parameter cannot work. What I believe should work is nesting the cleanup function within my working function so local variables in the enclosing function are available to the clean up function. This however does not work.

我的问题是:谁能看到一种使清理函数仅使用一个布尔值的方法,以便它可以在正常清理和异常清理之间进行选择?

My question is: can anyone see a way to make just a single boolean available to a clean function so it can select between normal and abnormal clean up?

这里有一些我认为应该可以使用的人为设计示例代码.

Here is some contrived example code that I believe should work.

function do_tuning
  % Set this false to cause a warning messages for control-C or MATLAB coding errors.
  normalTermination = false;
  oc = onCleanup(@() my_clean_up());
  tuningError = tuning_function()
  if tuningError
    % Set this true to suppress the warning message if a predictable error occurs.
    normalTermination = true;
    error('tuningError')
  end
  % Set this true to suppress the warning message if the function runs to completion.
  normalTermination = true;

  function my_clean_up
    if ~normalTermination
      disp('Warning: clean up failed. Please clean up manually');
    end
  end
end

按照此模式运行真实代码会导致错误:

Running real code following this pattern causes the error:

Undefined function or variable "normalTermination".

推荐答案

onCleanup 对象从 do_tuning 函数的工作空间外部调用该函数,因此使用嵌套函数将无济于事...影响文档状态:

The onCleanup object calls the function from outside the workspace of the do_tuning function so using a nested function will not help...Infact the documetation states:

更好的解决方案是更改问题并在my_clean_up函数中进行所有清理,这可能需要它确定是否需要执行(或具有可以始终应用的一般行为)

[edit - based on comments] A better solution would be to change the problem and have all cleaning up done within the my_clean_up function, which may require it to determine if what needs doing (or to have generic behaviour which can always be applied)

如果我们忽略此警告并解决问题...

If we disregard this warning and solve the question...

使用 global 变量,因此对于上面的示例代码.首先将变量定义为全局之前,然后将其初始设置为false(否则该变量可能会被覆盖)

Passing varables between workspaces is easiest with global variables so for the example code above.Firstly define the variable as global before initially setting it to false (otherwise the variable may be overwritten)

function do_tuning
    %# snip
    global normalTermination
    normalTermination = false;
    %# snip

在my_clean_up回调函数之前中,将变量定义为全局变量,然后使用它来检索值

Secondly define the variable as global in the my_clean_up callback function before using it to retrieve the value

function my_clean_up
    global normalTermination
    if ~normalTermination
    %# snip

警告与全局变量一样,很容易在错误的时间在其他位置编辑全局变量的值.

Warning as ever with global variables this is susceptible to the value of the global variable being edited elsewhere at the wrong time.

这篇关于如何将数据传递给MATLAB oncleanup函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:06