更好的工作流程是将这些脚本转换为模块化函数,接收可配置参数作为输入,而不是硬编码代码中的值。
这样您在每个 MATLAB 会话中调用相同的函数而不对 M 文件进行任何更改,只有每个会话根据需要传递不同的输入参数。
要详细了解 MATLAB 如何检测 M 文件中的更改,请运行以下命令:
>> help changeNotification
>> help changeNotificationAdvanced
您可能还想了解以下函数:rehash 和 clear functions
编辑:
找出哪些脚本/函数当前“加载到内存中”的一种方法是使用inmem。例如,我们将以下脚本保存在路径上可用的文件中(函数同样适用):
testScript.m
x = 10;
disp(x)
现在从一个干净的会话开始,脚本最初没有加载。调用脚本后,文件被加载并保留在内存中:
% initially not loaded
>> ismember('testScript', inmem())
ans =
0
% execute script
>> testScript
10
% file is cached in memory
>> ismember('testScript', inmem())
ans =
1
立即继续同一会话,对文件进行编辑(例如将x 更改为99)。通过再次检查加载的函数/脚本列表,您将看到 MATLAB 已经检测到更改,并通过将缓存版本从内存中删除来使其无效:
>> % .. make changes to testScript.m file
% file is automatically unloaded
>> ismember('testScript', inmem())
ans =
0
% execute the new script
>> testScript
99
% the result is cached once more
>> ismember('testScript', inmem())
ans =
1
我在我的 Windows 机器上测试了上述内容,但我不能保证这种行为是跨平台的,你必须在 Mac/Linux 上测试它,看看是否能正常工作......