Re: [問題] 自動載入及自動存檔

看板Mathematica作者 (oversky0)時間12年前 (2011/10/18 13:14), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串3/4 (看更多)
※ 引述《LPH66 (-858993460)》之銘言: : ※ 引述《oversky0 (oversky0)》之銘言: : : 兩個問題請教 : : 1.我有一些自定函數想要每次開新的 .nb 就自行載入, : : 請問有類似 dos 下 autoexec.bat 的功能嗎? : 首先 Mathematica 裡有兩個目錄是可以放啟動內容的: : $BaseDirectory systemwide base directory for files to be loaded by : Mathematica : $UserBaseDirectory user-specific base directory for files to be loaded by : Mathematica : 它裡面有幾個不同功能的資料夾: : Applications Mathematica application packages : Autoload packages to be autoloaded on startup : FrontEnd front end initialization files : Kernel kernel initialization files : Licensing license management files : SystemFiles general system files : 我們要的就是在其中的 Kernel 資料夾裡放入啟動指令: : Kernel/init.m run when the kernel is started : Kernel/end.m run when the kernel is terminated : 也就是說 你只要在 Kernel/init.m 裡放入指令 : 那每次啟動核心時就會自動執行其中的內容 : (以上內容節錄改寫自 tutorial/MathematicaFileOrganization) : 我自己是在 $BaseDirectory/Kernel/init.m 裡放了一行 << : 引入一個放在我自己方便修改的資料夾的 .m : (不然每次都要開 $BaseDirectory 有點煩...) : 裡面放了不少我自己寫的捷徑函式方便我自己使用 還是有些問題,再請教一下, 我現在是修改 /Kernel/Packages/init.m (** User Mathematica initialization file **) (** Decide how to display graphics on this machine **) Begin["System`Private`"] (* Hide any symbols which are created *) Which[ $Linked || $ParentLink =!= Null, <<"FrontEndGraphics.m", True, <<"NoGraphics.m" ] <<"UserFunction.m" End[] 紅色是我加入的部份, 然後在同一目錄下自建的 UserFunction.m (* ::Package:: *) (* ::Input:: *) (*Attributes[xPrint]={HoldAll,Listable};*) (*xPrint[x_]:=(Print[HoldForm[x]," =",Tab,x]);*) 接著開一個新的 notebook a = 3; xPrint[a] 跑出的結果是 xPrint[3] 正常應該是 a = 3 是不是我引用 UserFunction.m 的方式有錯? 再問個延伸問題,這種方式碰到 Remove["Global`*"] 會失效嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.128.221.134 ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:14) ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:16) ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:23) ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:24) ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:24) ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 13:28)

10/18 13:39, , 1F
你把程式寫進註解裡去了... Mathematica 的註解是 (* *)
10/18 13:39, 1F

10/18 13:40, , 2F
把註解拿掉應該就正常了
10/18 13:40, 2F
謝謝,後來發現在存成 .m 時, 要先 Initialization Cell, 否則就會有 (* *)。 不過這方法我試時碰到 Remove["Global`*"] 就會被清掉。 還是有其它會清所有變數但不會清函數的方法嗎? ※ 編輯: oversky0 來自: 220.128.221.134 (10/18 15:09)

10/18 18:01, , 3F
Quit[]
10/18 18:01, 3F

10/19 10:26, , 4F
我試的結果 Quit[]也會把函數清掉。
10/19 10:26, 4F

10/19 10:42, , 5F
Remove, Clear, ClearAll, Quit 這幾個有何差別?
10/19 10:42, 5F
目前試出的解決方案是在 init.m 裏用 Protect[] 來保護設定的函數, 如果要清除所有變數時,用 Remove["Global`*"] ※ 編輯: oversky0 來自: 220.128.221.133 (10/19 14:11)
文章代碼(AID): #1EdGiq2u (Mathematica)
討論串 (同標題文章)
文章代碼(AID): #1EdGiq2u (Mathematica)