Executable Octave Programs

Hello!
I have witten my program with Octave and now I am going to convert it into an executable file so that users can invoke it without reading the code. The Online Manual suggests using the ‘ #! ’ script mechanism. By the way, I have two questions:

  1. Should I install GNU systems or Unix systems?
  2. I would like to have further information about how to use the ‘#! ’ script mechanism.

Kind regards.

You should know if your user are on GNU systems or Unix systems.

#! is also called “shebang” (or “hashbang”). Use your favorite search engine to get further information on it. There are a ton of descriptions out there already.

Thank you mmuetzel for your fast reply.

  1. My users are on Window system. Is it ok?
  2. I would like to know if I have to write the octave scripts at the beginning of my code or in a different programming environment.

Regards.

Windows is neither GNU nor Unix.
Afaict, there is no shebang mechanism on Windows.
You could however use batch files to start Octave with any given script you wrote.

Are you are referring to the Executable Octave Programs section of the user manual?

This capability has nothing to do with compiled binary programs or being able to prevent users from accessing the source code. An Octave executable program is simply a plain text script that runs itself using the Octave runtime.

Refer also to this previous discussion topic:

Thank you mtmiller for your reply.

I will try to explain my problem in a better way.

Now, whan I need to work with my Octave program, I have to follow the following steps.

  1. I open my Octave file.

  2. I push the button “Save File and Run”.

  3. The system opens the GUI which have created.

  4. I do my job through the GUI.

My aim is to skip the ponts 1 and 2. In different words, when I click twice on my file, I would like to see directly the GUI.

mmuetzel suggests using BATCH FILE.

Thanks for detailing what you’d like to do.

A batch file with something similar to the following command might come close to what you describe:

C:\path\to\where\octave\is\installed\octave.vbs --no-gui --eval uiwait(figure())

Obviously, you’d need to adjust C:\path\to\where\octave\is\installed to fit your system. And instead of figure() you’d call the function that opens your GUI and returns a handle to its main window.
Make sure that function is in your load path by either adding an addpath command to your startup file or add -p C:\path\to\your\GUI\files to the command in the batch file.

Ok. So I have to write the path of Octave and the path of my file.
Probably, I also have to use commands such as @echo off, set and start.
Could you provide me with further deetails about how to organize my bath file?

A batch file with that single line should be enough.

Did you try if it works? If it doesn’t, which error are you facing?

Following your advice, I have written the following lines in my batch file.

C:\Octave-6.3.0\octave.vbs --no-gui --eval uiwait(myscript.m)

-p C:\Users\Nicola\Desktop\09_Programma\Prova

When I click twice on it, the system opens two DOS windows which disappear after few seconds.

Is myscript a function that returns a structure which stores a figure handle to the main GUI in the field m?

The command line parameter -p ... should be part of the command. Write everything in one single line.

myscript is a test function. Its code is:
%myscript.m
x = 1:500;
y = cumsum(rand(size(x)) - 0.5);
plot(x,y)
print -dpng -r0 out.png

I have written the commands in a single line but the batch file doesn’t work yet.

There might be a few misconceptions here:
Starting Octave with the command line argument --eval starts Octave, evaluates the corresponding command and closes Octave when that evaluation is done.

The function uiwait takes a graphics handle as its input argument and “stalls” execution as long as that graphics handle is valid – thus, delaying closing Octave. See doc uiwait.

So, your batch file starts Octave and tries to evaluate uiwait(myscript.m). Like you can confirm by evaluating that command manually at the command prompt, that results in an error.
After that, execution continues and – since nothing else is to be done ( no try ... catch or whatever…) – Octave closes.

Maybe you wanted to run the script myscript.m (it’s not a function!)?
That could be done e.g. with the following line in the batch file:

C:\Octave-6.3.0\octave.vbs --no-gui -p C:\Users\Nicola\Desktop\09_Programma\Prova --eval myscript

I have put your suggestion into practice. However, now I have a different problem.
When I click twice on my batch file, it doesn’t open up.

What do you mean by “it doesn’t open up”?
Does it save the figure with the random data to out.png in the folder with the batch file?

It means that I can’t open my program. When, I click twice on my batch file, It doesn’t give me any response.

No, it doesn’t save the figure.

Can you attach the batch file you are using?
Dragging and dropping the file into the text box should be working.

myscript.m (97 Bytes)
run_octave_script.bat (140 Bytes)
run_octave_script.bat.text (140 Bytes)

I’m not sure if this is something that happened when uploading the file. But when I download it, it has the file extension .bat.text (in both of the above links).
Make sure the file extension is .bat.

Additionally, Octave is installed in a directory with spaces for you. So, you’ll have to surround the path to the .vbs file with double quotes (").

Last but not least: Command line parameters have to be separated by whitespace characters (or is it only spaces?).

I tried to fix these error in this file:
run_octave_script.bat (143 Bytes)

Does it work with that?

Dear mmuetzel, thank you for your availability.

Your batch file works perfectly with the test function myscript.m

Now, I am applying the same methodology to a more complex function and I am experiencing again some difficulties. The problem is that, when I click twice on my bath file, the system opens a DOS window which appears and soon after disappears.

My function includes many subfunctions. Probably, the problem stems from that.