本文介绍了为自定义Mupad程序定义一般相对搜索路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我在路径'C:\projectFolder\ABC\abc\'上有一个mupad笔记本 myMupadNotebook.mn .它调用位于'C:\DEF\GHI\'的过程 MyMupadProcedure.mu .

Imagine I have a mupad-notebook myMupadNotebook.mn at the path 'C:\projectFolder\ABC\abc\'. It calls the procedure MyMupadProcedure.mu which is located at 'C:\DEF\GHI\'.

现在,我在'C:\projectFolder\XYZ\xyz\'上有一个Matlab脚本 main.m ,内容如下:

Now I have a Matlab script main.m at 'C:\projectFolder\XYZ\xyz\' with the content:

nb = mupad('C:\projectFolder\ABC\abc\myMupadNotebook.mn');
status = evaluateMuPADNotebook(nb);

因此它将初始化符号引擎并执行Mupad脚本.但是Mupad脚本需要知道在哪里可以找到该过程.因此,我可以使用文件->属性->启动命令在Mupad Notebook中定义一些启动命令(或启动脚本),如下所示:

So it initializes a symbolic engine and executes the Mupad script. But the Mupad script requires to know where to find the procedure. So I can define some start-up commands (or a start-up script) within the Mupad Notebook with File->Properties->Start-up commands like this:

READPATH := "C:\DEF\GHI\";
read("MyMupadProcedure.mu");


但是现在我在不同的计算机上工作,并且绝对文件夹路径不同,但是相对路径相同.如何在所有计算机上使用我的脚本?


But now I work on different machines and the absolute folder paths are different, but the relative paths are the same. How can I use my scripts on all machines?

在Matlab中,我只需要在每台计算机上设置 SearchPath 即可,它是否适用于Mupad?

In Matlab I'd just set the SearchPath on every machine and it works, is there something equivalent for Mupad?

或者,如果我可以将一个字符串从Matlab传递给Mupad,这也将有所帮助,我只需将启动命令写在笔记本的标头中,并使用Matlab函数确定相对路径.但是以下几行的所有组合都不起作用:

Alternatively it would already help if I could pass a string from Matlab to Mupad and I'd just write the start-up commands in the header of my notebook and determine the relative path with Matlab functions. But all combinations of the following lines just do not work:

syms X
X = 'hello'
setVar(nb,'X',X)
evalin(nb,['X := "' X '"']) 

推荐答案

有人会认为MuPad与Matlab的集成要好得多.

One could think the integration of MuPad into Matlab is much better.

除了符号表达式(setVar)之外,从变量和字符串从Matlab到MuPad的直接传递似乎是不可能的.如我错了请纠正我.但是,可以在Matlab中使用相对路径来写入文件,而在MuPad中使用相对路径来读取文件.

The direct transfer from variables and strings from Matlab to MuPad, apart from symbolic expressions (setVar), does not seem to be possible. Correct me if I'm wrong. However it is possible to write files in Matlab with a relative path and read files in MuPad with a relative path.

这样,可以将存储MuPad过程的路径写入文本文件-位于执行MuPad Notebook的同一文件夹中:

This way it is possible to write the path, where the MuPad procedures are stored, into a textfile - located in the same folder, where the MuPad Notebook is executed:

%// determined with pwd, cd and string manipulation etc
MuPadNotebookPath = 'C:\projectFolder\ABC\abc\' 
MuPadProceduresPath = 'C:\DEF\GHI\';    

fid = fopen( [MuPadNotebookPath  '\parameters.txt'], 'w'); 
fprintf(fid,'%s\r\n%', strrep(MuPadProceduresPath ,'\','\\')); %'
fclose(fid);

现在'C:\projectFolder\ABC\abc\'中将有一个文件 parameters.txt .

在MuPad中,环境变量 NOTEBOOKPATH 可用于获取 parameters.txt myMupadNotebook.mn 的目录.

In MuPad the environment variable NOTEBOOKPATH can be used to get the directory of both the parameters.txt and myMupadNotebook.mn.

ftextinput 然后用于从文本文件读取路径'C:\DEF\GHI\'.最后,可以设置 READPATH

ftextinput can then be used to read to path 'C:\DEF\GHI\' from the text file. Finally the READPATH can be set.

cfgfile := NOTEBOOKPATH . "parameters.txt":
rpath = ftextinput(cfgfile, rpath):
READPATH := rpath:
read("MyMupadProcedure.mu");

总共看起来像:

nb = mupad(MuPadNotebookPath);
fid = fopen( [MuPadNotebookPath  '\parameters.txt'], 'w'); 
fprintf(fid,'%s\r\n%', strrep(MuPadProceduresPath ,'\','\\')); %'
fclose(fid);
status = evaluateMuPADNotebook(nb);

这篇关于为自定义Mupad程序定义一般相对搜索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:09