I'm relatively new to using Octave. I'm working on a project that requires me to collect the RGB values of all the pixels in a particular image and compare them to a list of other values. This is a time-consuming process that takes about half a minute to run. As I make edits to my code and test it, I find it annoying that I need to wait for 30 seconds to see if my updates work or not. Is there a way where I can run the code once at first to load the data I need and then set up an artificial starting point so that when I rerun the code (or input something into the command window) it only runs a desired section (the section after the time-consuming part) leaving the untouched data intact?
-
smth like image diff ? https://stackoverflow.com/questions/5132749/diff-an-image-using-imagemagick – Adam Jul 27 '22 at 19:10
-
Not exactly. Instead of looking for and isolating differences, I am trying to find a way to rerun my code while keeping the same data loaded, so that it doesn't need to be reloaded each time. My code starts with lines like clc; close all; clear; which erase all variables/figures/etc. I want to find a way for some data to not get cleared out by those commands as reloading them takes a lot of time – motelchicago Jul 27 '22 at 19:24
-
You can create second array – Adam Jul 27 '22 at 20:52
-
Save/load your array/s or create sections in your code – Sardar Usama Jul 27 '22 at 21:27
-
1Welcome to Stack Overflow! Please read the descriptions of both [tag:matlab] and [tag:octave]. They are **not** the same, thus please only use both tags when asking about the similarities/differences between the two. Using both tags could result in answers being incompatible to the other software, unnecessarily forcing answerers to check validity of their code in both programs. – Adriaan Jul 28 '22 at 09:23
-
Maybe use `save` just before that point, then replace all the previous code with `load` (keep a backup of the code elsewhere). – Cris Luengo Jul 28 '22 at 14:22
-
1It sure what Octave Forge has to do with any of this. Forge is the repository where you fetch toolboxes. – Cris Luengo Jul 28 '22 at 14:22
-
One more thing: if collecting colors from a single image takes 30 seconds, you should rethink your code. Learn about vectorizing! – Cris Luengo Jul 28 '22 at 14:40
2 Answers
You may set your variable to save into a global variable,
and then use clear -v
instead of clear all
.
clear all
is a kind of atomic bomb, loved by many users. I have never understood why. Hopefully, it does not close the session: Still some job for quit()
;-)
To illustrate the proposed solution:
>> a = rand(1,3)
a =
0.776777 0.042049 0.221082
>> global a
>> clear -v
>> a
error: 'a' undefined near line 1, column 1
>> global a
>> a
a =
0.776777 0.042049 0.221082

- 791
- 3
- 16
Octave works in an interactive session. If you run your script in a new Octave session each time, you will have to re-compute all your values each time. But you can also start Octave and then run your script at the interactive terminal. At the end of the script, the workspace will contain all the variables your script used. You can type individual statements at the interactive terminal prompt, which use and modify these variables, just like running a script one line at the time.
You can also set breakpoints. You can set a breakpoint at any point in your script, then run your script. The script will run until the breakpoint, then the interactive terminal will become active and you can work with the variables as they are at that point.
If you don't like the interactive stuff, you can also write a script this way:
clear
if 1
% Section 1
% ... do some computations here
save my_data
else
load my_data
end
% Section 2
% ... do some more computations here
When you run the script, Section 1 will be run, and the results saved to file. Now change the 1
to 0
, and then run the script again. This time, Section 1 will be skipped, and the previously saved variables will be loaded.

- 55,762
- 10
- 62
- 120