MATLAB: alternative of Octave's function 'source' (import m-file)

烈酒焚心 提交于 2019-12-24 10:35:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!