【octave】nested functions not implemented in this context解決方法

nested functions not implemented in this context解決方法

1.問題產生

在處理Coursera course - Algorithms for Battery Management System - Part 3 - Battery State of Charge Estimation - Week 7 - Capstone 2 - Tuning SPKF時,內嵌函數在jupyter notebook的octave環境下運行無報錯,在octave GUI運行時報錯nested functions not implemented in this context
代碼:SOCbySPKF-CapstoneProjectPart2.m

2.內嵌函數

octave官方的解釋:Nested Functions
例子:

function y = foo ()
  x = 10;
  bar ();
  y = x;

  function bar ()
    x = 20;
  endfunction
endfunction

foo ()
 ⇒ 20

3.解決方式

出現問題的原因在於:內含內嵌函數的函數必須作爲一個函數文件,不能寫在腳本里。下面例子中,前者報錯,後者不報錯。

clear -all; 
clc;

function test
  function nested1
     disp('nested 1')
  end

  function nested2
     disp('nested 2')
  end

   nested1
   nested2
end
function test
  function nested1
     disp('nested 1')
  end

  function nested2
     disp('nested 2')
  end

   nested1
   nested2
end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章