代码之家  ›  专栏  ›  技术社区  ›  Dev-iL

调试时如何使用曲线拟合工具?

  •  2
  • Dev-iL  · 技术社区  · 7 年前

    Matlab的曲线拟合应用程序(以前称为“工具”,因此, cftool )是用于交互式曲线拟合的图形工具 1 .

    使用此工具的一般方法是从工作区中选择变量:

    enter image description here 但是,在调试期间,数据选择被禁用(此 documented ):

    enter image description here

    ……这是非常麻烦的,因为我们必须将数据保存到一个文件中,并且在重新加载并在中使用此数据之前,要么退出调试,要么打开一个新的matlab实例。 CFCODE .

    我假设禁用输入的原因是,在调试期间,我们通常有多个工作区,因此在UX方面,迭代这些工作区或为用户提供工作区选择过于繁琐,因此开发人员决定禁用输入,直到只有一个工作区存在为止。

    我的问题是: 如何禁用 CFCODE 或者指定我们感兴趣的工作区,以便使用 CFCODE 在调试期间?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Dev-iL    7 年前

    我挖了一些洞,我发现了:

    • 曲线拟合工具包含一种特殊类型的组合框,用于选择使用 com.mathworks.mlservices.MatlabDebugObserver 类以检测调试模式并禁用控件。这些控件的Java类是

      MATLAB\R20###\java\jar\toolbox\curvefit.jar!
               com.mathworks.toolbox.curvefit.surfacefitting.SFDataCombo
      

      我的发现是:

      A)开始 cftool 把把手放在窗户上,

      hSFT = getappdata( groot, 'SurfaceFittingToolHandle' );
      

      b)探索 hSFT 要找到包含对象的Java对象,我们在其中指定FIT数据。

      c)定位 .jar 包含上述Java类的文件,使用命令 src :

      jObj.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
      
    • 我们可以通过访问各个组合框并调用它们来禁用调试侦听器。 cleanup() 方法,它删除调试侦听器(请参见下面代码中有关此问题的说明)。这涉及到访问多个对象的私有字段,为此我们将使用反射:

      function unlockCftool()
      % NOTES: 
      % 1) After unlocking cftool, it will no longer update the list of workspace variables, so 
      % make sure all desired variables exist in the base workspace before proceeding, or you'll 
      % need to restart cftool.
      % 2) DO NOT execute this code while debugging, since then the variable selection fields in
      % cftool will be stuck in their disabled mode until it is restarted.
      
      hSFT = getappdata( groot, 'SurfaceFittingToolHandle' );
      jEFP = hSFT.FitFigures{1}.HFittingPanel.HUIPanel.Children.java.getJavaPeer();
      f = jEFP.getClass().getDeclaredField('fittingDataPanel');
      f.setAccessible(true);
      jFDP = f.get(jEFP);
      f = jFDP.getClass().getDeclaredFields(); f = f(1:4); % <- shortcut for:
      %{
      f = [jFDP.getClass().getDeclaredField('fXDataCombo');
           jFDP.getClass().getDeclaredField('fYDataCombo');
           jFDP.getClass().getDeclaredField('fZDataCombo');
           jFDP.getClass().getDeclaredField('fWDataCombo')];
      %}
      java.lang.reflect.AccessibleObject.setAccessible(f, true);
      for ind1 = 1:numel(f)
        f(ind1).get(jFDP).cleanup();
      end
      

    所以现在我们可以做以下的事情:

    X = 0:9;
    Y = 10:-1:1;
    cftool();
    % <select the X and Y variables in cftool to get a decreasing slope>.
    unlockCftool();
    % <enter debug mode, for example using: dbstop in unlockCftool; unlockCftool(); >
    assignin('base', 'X', 5:-1:-4);
    % <re-select X to update the data - resulting in a rising slope>.
    
    推荐文章