问题
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\'
.
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);
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?
In Matlab I'd just set the SearchPath on every machine and it works, is there something equivalent for Mupad?
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 '"'])
回答1:
One could think the integration of MuPad into Matlab is much better.
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.
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);
Now there will be a file parameters.txt in 'C:\projectFolder\ABC\abc\'
.
In MuPad the environment variable NOTEBOOKPATH can be used to get the directory of both the parameters.txt and myMupadNotebook.mn.
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");
In total it looks like:
nb = mupad(MuPadNotebookPath);
fid = fopen( [MuPadNotebookPath '\parameters.txt'], 'w');
fprintf(fid,'%s\r\n%', strrep(MuPadProceduresPath ,'\','\\')); %'
fclose(fid);
status = evaluateMuPADNotebook(nb);
来源:https://stackoverflow.com/questions/31786164/define-general-relative-search-path-for-custom-mupad-procedures