简体   繁体   中英

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. How to load the lines to octave's workspace?

For example, suppose the contents of somedefinitions.m is

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).

How to do this? I tried load somedefinitions.m but this says it cannot determine the file format.

You can not load somedefinitions because its not data. If you call the file somedefinitions.m but do not make it a function, then you can call somedefinitions in your main code. That will execute everything in somedefinitions.m and load it in the workspace. Then you can do whatever you want in the command 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. 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:

The save and load command can be used to store the state of your workspace.

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 myvariables.mat

this will save both c and d into the file mentioned. They can be retrieved back in to the workspace later by using 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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