简体   繁体   English

使用批处理中的一个参数运行 Octave 函数

[英]Run Octave function with one argument from batch

I am trying to run an octave function from a batch file.我正在尝试从批处理文件运行八度函数。 The function is well written given how it works when launched from within the Octave GUI.考虑到从 Octave GUI 中启动时的工作方式,该函数写得很好。 The batch file, other than pointing to the octave function, defines the only argument needed by it.除了指向八度函数之外,批处理文件定义了它所需的唯一参数。 A while back this was not a function but a simple Octave script and the commands used were ok.不久前,这不是一个函数,而是一个简单的 Octave 脚本,使用的命令还可以。 The only issue I am encountering now is being able to pass the variable calculated by the batch file onto the octave function.我现在遇到的唯一问题是能够将批处理文件计算的变量传递给八度函数。

I have recently written an octave function to do some file management.我最近写了一个八度函数来做一些文件管理。 It requires only one input from the user:它只需要来自用户的一个输入:

function replace_TMM (file_base)

where file_base is a string to specify what directory I am working on.其中 file_base 是一个字符串,用于指定我正在处理的目录。 So it has to be something like "Z:" or "I:" or so on.所以它必须是类似“Z:”或“I:”之类的东西。 I am quite sure that the function is well written since I am able to use it from Octave GUI without any issues.我很确定这个函数写得很好,因为我可以毫无问题地从 Octave GUI 使用它。 The fact is that I would like to run this function from a batch file.事实上,我想从批处理文件中运行这个函数。 Inside this batch file I wrote:在这个批处理文件中,我写道:

SET a=%cd:~0,2%

This command is able to identify the working directory so "a" will be equal to "Z:" or similars.此命令能够识别工作目录,因此“a”将等于“Z:”或类似的。 Now my issue is telling the batch file to evaluate the octave function using "a" as its input argument.现在我的问题是告诉批处理文件使用“a”作为输入参数来计算八度函数。 I tried stuff like:我试过类似的东西:

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval _03_REPLACE_V04("'%cd:~0,2%'")

which does not seem to work.这似乎不起作用。 This kind of solution gives a syntax error at batch level, it is not even able to enter the octave file.这种解决方案在批处理级别给出语法错误,甚至无法输入八度文件。

If I instead try something like:如果我改为尝试类似的方法:

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q _03_REPLACE_V04.m Z:

It is able to enter the octave file but it does not process the function, just skips over it to get to the end of the script.它能够输入八度文件,但它不处理函数,只是跳过它到达脚本的末尾。

Same goes if I try the following:如果我尝试以下操作,情况也是如此:

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" _03_REPLACE_V04.m -"Z:"

In brief I bvelieve that the function itself works, it is only a matter of passing a variable from the batch to the octave.简而言之,我相信函数本身可以工作,这只是将变量从批处理传递到八度音程的问题。

Would really appreciate some help, thanks in advance.非常感谢一些帮助,在此先感谢。

UPDATE 1更新 1

I have done what was suggested by @Dariush Gavari and used the following syntax:我已经完成了@Dariush Gavari 的建议并使用了以下语法:

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval "replace_TMM('%a%')"

This gets me the following error message:这让我收到以下错误消息:

error: 'replace_TMM' undefined near line 1, column 1

I believed that it was because it was not ablòe to find the script containing the function.我认为这是因为无法找到包含该函数的脚本。 This is saved in a file called _03_REPLACE_V04.m For this reason I have tried with这保存在一个名为 _03_REPLACE_V04.m 的文件中因此我尝试了

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval _03_REPLACE_V04.m "replace_TMM('%a%')"

Leading to the following error:导致以下错误:

error: --eval "CODE" and script file are mutually exclusive options

usage: octave [-HVWdfhiqvx] [--debug] [--doc-cache-file file] [--echo-commands]
       [--eval CODE] [--exec-path path] [--experimental-terminal-widget]
       [--gui] [--help] [--image-path path] [--info-file file]
       [--info-program prog] [--interactive] [--line-editing] [--no-gui]
       [--no-history] [--no-init-file] [--no-init-path] [--no-line-editing]
       [--no-site-file] [--no-window-system] [--norc] [-p path]
       [--path path] [--persist] [--server] [--silent] [--traditional]
       [--verbose] [--version] [file]

I believed that the problem could also have been having the functional nd the file with two different names.我认为问题也可能出在函数和具有两个不同名称的文件上。 To solve this I have kept the same file name but changed the function to match it:为了解决这个问题,我保留了相同的文件名,但更改了函数以匹配它:

function _03_REPLACE_V04 (file_base)

Then in the batch:然后在批处理中:

"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval "_03_REPLACE_V04('%a%')"

Leading to:导致:

warning: function '_03_REPLACE_V04' defined within script file '\99_TOOLS\OCTAVE_FILES\_03_REPLACE_V04.m'
error: invalid call to script \99_TOOLS\OCTAVE_FILES\_03_REPLACE_V04.m
error: called from
    _03_REPLACE_V04

In other words still no way of making it work.换句话说,仍然没有办法让它发挥作用。 :) :)

Octave provides the argv function, which returns a cellstring array of all arguments passed to the octave executable at the time of launch, or in the case where it was used to launch a script, then this is the script's arguments. Octave 提供了argv函数,它返回一个 cellstring 数组,其中包含在启动时传递给 octave 可执行文件的所有参数,或者在它用于启动脚本的情况下,这就是脚本的参数。

So presumably all you have to do to get your directory from within octave is argv(){1}所以大概你需要做的就是从 Octave 中获取你的目录是argv(){1}

If you would like to convert your filename to an absolute filename, you could also do this from within octave via the make_absolute_filename function.如果您想将文件名转换为绝对文件名,您也可以通过make_absolute_filename函数在 Octave 内执行此操作。


Incidentally, a very useful command you should know of in octave is the lookfor command.顺便说一下,在 Octave 中你应该知道的一个非常有用的命令是lookfor命令。 Writing lookfor arguments in the terminal returns a list with all functions which have the word "arguments" in their description;在终端中编写lookfor arguments会返回一个列表,其中包含所有在其描述中包含“arguments”一词的函数; argv is at the top of that list. argv在该列表的顶部。

You can then do help argv to see more details on that command.然后您可以执行help argv以查看有关该命令的更多详细信息。

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

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