问题
In Octave you can import a m-file source the function 'source' (see http://octave.sourceforge.net/octave/function/source.html). I use it to create a lot of configuration parameters and source it from the main file.
MATLAB has no function 'source' and I cannot find an alternative. Do you have any idea?
回答1:
Just calling an m-file from a MATLAB script will import it into the script.
e.g. If I have a script configparameters.m where I define a=2, the script (in the same directory)
clear all;
configparameters;
disp(a)
will print the value 2 to the screen.
回答2:
run should pretty much be what you're looking for, if I understand the source
documentation correctly.
EDIT:
It seems run refuses to run files other than m-files (and probably p-files etc.).
So probably need two functions in matlab (which you can obviously put together in your own source
implementation):
eval(fileread(<filename>))
This reads the full text contained in filename
and evaluates it.
Just as source
apparently does.
来源:https://stackoverflow.com/questions/27315668/matlab-alternative-of-octaves-function-source-import-m-file