1

I would like to know if it possible to pass directly a custom argument when executing an octave script from Powershell/batch in general. Lets make an example:

$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli.exe"

# Executing the script
& $octaveExePath test.m "some_custom_string"

is there a way to receive the custom argument from inside the test.m script?

EDIT 1

I've done what suggested by @Ander without succes, I suppose that in this situation matlab and octave beaviuor are really different. My result making and example.m file

function []=example(arg1)
 disp(arg1)
endfunction

then calling it from powershell

& "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli-7.1.0.exe" example.m asd

Return the error

error: 'arg1' undefined near line 2, column 7
error: called from
    example at line 2 column 2

EDIT 2

To be more specific my goal is to know if there is a way to pass argument from a shell directly to an octave program without using some external file as done for example in this artcile

Gam
  • 318
  • 2
  • 9
  • I am not sure for octave, but in matlab this is easy: https://stackoverflow.com/questions/8981168/running-a-matlab-program-with-arguments – Ander Biguri May 23 '22 at 10:33
  • If it can be done at the command prompt, you should be able to do it in PowerShell. What problem are you experiencing? Is the "some_custom_string" not getting passed to the octave script? If so, then maybe the stop-parsing token (--%) would help: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.2#the-stop-parsing-token – Darin May 23 '22 at 11:44
  • 1
    @Gam I don't have time for a full answer now, but in general octave scripts can access arguments passed to them via the `argv` command inside the script. Also, matlab has no ability to 'execute' scripts; the linked approach simply evaluates code in a matlab session. You can try this approach in octave too if you want; the equivalent option in octave is `--eval` rather than `-r`. I'm not too familiar with batch/powershell, but perhaps the manual section on using bash might be of use; I imagine the solution is similar: https://octave.org/doc/v7.1.0/Passing-Arguments-to-Executable-Scripts.html – Tasos Papastylianou May 24 '22 at 15:41
  • One difficulty here is that batch and command-line argument passing only work with strings, but Matlab has various datatypes, like numbers, strings, structs, cells, and so on. If you have nontrivial arguments, you might consider writing a wrapper script or function that takes an argument list _as a single string containing an M-code expression_, then reads that with Matlab's `eval()` function, and uses the results as the arguments to your main script or function. – Andrew Janke May 31 '22 at 08:44

1 Answers1

1

I've elaborated a simple workaround that enable passing variables to an octave script while working in a Windows environment. The problem is that under Windows is not possible to create Octave executable programs as said in the documentation.

Anyway this workaround is somehow near to what suggested by @Tasos to which my deepest thanks go to.

Let's say we have a tentative.m file with a single line of code:

disp(external_variable)

without the variable declaration.

Outside we execute just a few PowerShell lines of code in sequence (be sure that the position of PowerShell is the same of where you created the script):

$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-7.1.0.exe"
$externalVariable = "just a little try"

& $octaveExePath --eval "external_variable='$($externalVariable)'; tentative" 

this result with the output:

just a little try

So, in this way I have successfully "passed" an external value to an octave script through external execution "bypassing" the fact that --eval and the FILE command line options are mutually exclusive (for what I know). The only point of warning is that while using this technique the non initialized variable (in the script) must be coordinated with the variable name evaluated externally (the names must be the same).

halfer
  • 19,824
  • 17
  • 99
  • 186
Gam
  • 318
  • 2
  • 9
  • If you make `tentative` a function M-file instead of a script, taking `external_variable` as input argument, the you can `--eval "tentative('$($externalVariable)')"` – Cris Luengo Oct 16 '22 at 14:18