简体   繁体   English

将 am 文件中的定义加载到 GNU Octave 工作区

[英]Load definitions in a .m file into GNU Octave workspace

I have written am file which has definition of many matrices.我写了一个定义了许多矩阵的文件。 Now, I want to add some of them, and do some other operations.现在,我想添加其中的一些,并进行一些其他操作。 Now I could write those operations in the file itself and run it in octave-cli to see the results, but I would like to load the definitions, and do the operations one by one in the input field of octave GUI (similar to what can be done in a mathematica notebook), but I don't want to type all the lines manually in octave gui once again.现在我可以在文件本身中编写这些操作并在 octave-cli 中运行以查看结果,但我想加载定义,并在 octave GUI 的输入字段中一一进行操作(类似于可以在数学笔记本中完成),但我不想再次在 octave gui 中手动输入所有行。 How to load the lines to octave's workspace?如何将线条加载到 octave 的工作区?

For example, suppose the contents of somedefinitions.m is例如,假设somedefinitions.m的内容是

function somedefinitions()
c = 4;
d = 5;

Now I want this to load into octave gui, and want to evaluate c+d, c*d etc. in the input field (but I don't want to manually write definitions of c and d in the octave-gui).现在我希望将其加载到 octave gui,并希望在输入字段中评估 c+d、c*d 等(但我不想在 octave-gui 中手动编写 c 和 d 的定义)。

How to do this?这个怎么做? I tried load somedefinitions.m but this says it cannot determine the file format.我尝试load somedefinitions.m但这表示它无法确定文件格式。

You can not load somedefinitions because its not data.您无法加载somedefinitions ,因为它不是数据。 If you call the file somedefinitions.m but do not make it a function, then you can call somedefinitions in your main code.如果您调用文件somedefinitions.m但不将其设为 function,那么您可以在主代码中调用somedefinitions That will execute everything in somedefinitions.m and load it in the workspace.这将执行somedefinitions.m中的所有内容并将其加载到工作区中。 Then you can do whatever you want in the command window.然后你可以在命令 window 中做任何你想做的事情。 You can not do it now because functions have their own workspace, so even if you would try it as it is not, variables would be defined inside somedefinitions but deleted when finished.你现在不能这样做,因为函数有自己的工作空间,所以即使你不这样做,变量也会在一些定义中somedefinitions ,但在完成时会被删除。 In short, delete the first line of your example, and just call that script.简而言之,删除示例的第一行,然后调用该脚本。

Just to add another way to use load in the way you originally intended:只是为了添加另一种以您最初预期的方式使用load的方式:

The save and load command can be used to store the state of your workspace. saveload命令可用于存储工作区的 state。

For example, if in your workspace you defined the variables:例如,如果您在工作区中定义了变量:

>> c = 4
c =  4

>> d = 5
d =  5

you could save both of these variables using the save command:您可以使用 save 命令保存这两个变量:

>> save myvariables.mat

this will save both c and d into the file mentioned.这会将cd保存到提到的文件中。 They can be retrieved back in to the workspace later by using load .稍后可以使用load将它们检索回工作区。 Eg:例如:

clearing the workspace and verifying it's empty:清除工作区并验证它是空的:

>> clear
>> whos

loading the saved variables back in:将保存的变量加载回:

>> load myvariables.mat
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        c           1x1                          8  double
        d           1x1                          8  double

after this you can perform whatever operations you want on the variables.在此之后,您可以对变量执行任何您想要的操作。

Ander's method above will also work.上面安德的方法也可以。 In that case you are saving a script file (not a function) that is just a list of commands saved in an m-file that Octave can then execute from the command line.在这种情况下,您将保存一个脚本文件(不是函数),它只是保存在 m 文件中的命令列表,然后 Octave 可以从命令行执行。 His method has the advantage that it's easy to modify the creation of the variables and it retains the method of their creation.他的方法的优点是很容易修改变量的创建,并且保留了变量的创建方法。 If the workspace contains the output of complex computations that took a lot of time, then maybe saving the workspace would be a timesaving route.如果工作空间包含需要大量时间的复杂计算的 output,那么保存工作空间可能是一种节省时间的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM