MATLAB编程(3)

您所在的位置:网站首页 matlab如何循环调用函数 MATLAB编程(3)

MATLAB编程(3)

2023-09-09 00:41| 来源: 网络整理| 查看: 265

问题描述:

       在做算法对比实验时,经常需要依次运行多个算法的代码。每个算法的入口程序是一个脚本.m文件(当然,算法的脚本文件中又会调用算法自己的子函数)。我们期望MATLAB依次运行这些对比算法的脚本.m文件,而不用等到一个算法的程序执行结束后,再去运行下一个算法的代码(有时还得修改路径),这样显得很麻烦。

       当然,运行前首先编写好这些脚本文件,确保没有错误,需要修改的都已经调整完毕。另外,每个算法都会涉及到调用子函数。所以不同算法可能会调用相同文件名的函数程序(可能函数文件名相同,但是其中的代码会有差异)。这时就要注意函数命名问题,最好避免相同函数文件出现,从而避免不必要的麻烦,或者带来不期望的结果。

简化问题:

       现在有多个脚本文件,文件名分别为“file1.m”、“file2.m”和“file3.m”,期望实现MATLAB依次运行这些个脚本.m文件。

解决方法:

方法一:新建一个脚本文件,编写下面的代码:

clc clear close all file1 % 第一个脚本文件的名字(注:直接写下脚本文件的名字,无需用单引号括起来,无需.m后缀。下同) clear % 清除第一个脚本文件执行过程中的所有变量 file2 % 第二个脚本文件的名字(要求同上) clear % 清除第二个脚本文件执行过程中的所有变量 file3 % 第三个脚本文件的名字(要求同上) % 下面如果还有其他脚本文件,则都可以如法炮制

方法二:使用 run() 函数

clc clear close all run( 'file1.m' ) % 运行第一个脚本文件 clear % 清除第一个脚本文件执行过程中的所有变量 run( 'file2.m' ) % 运行第二个脚本文件 clear % 清除第二个脚本文件执行过程中的所有变量 run( 'file3.m' ) % 运行第三个脚本文件 % 值得一提的是:这些个脚本文件可以在不同的文件夹下。比如一个在D盘某个文件夹下,一个在F盘某个文件夹下。 % 这时,只需要在run()语句的字符串中加入脚本文件的绝对路径即可。脚本文件执行时,也不会受到当前路径的影响。 % 例如:run( 'D:\file1.m' ); % 下面如果还有其他脚本文件,则都可以如法炮制

       注:尽管有时要运行的脚本文件不在MATLAB的当前路径下。只要在run()语句的字符串中加入脚本文件的绝对路径,则在脚本文件执行时,就不会受到当前路径的影响。

 

另外,可以使用命令“doc run”查看run函数的详细使用说明。

这里也摘抄过来:

run——Run MATLAB script(执行MATLAB脚本文件)

Syntax:

        run( scriptname )

Description:

        run( scriptname ); %  runs the MATLAB script specified by scriptname.(运行由scriptname指定的MATLAB脚本文件)

        % scriptname — Full or relative script path(character vector)

        % Full or relative script path to a MATLAB script, specified as a character vector. 

        % scriptname can specify any file type that MATLAB can execute, such as MATLAB script files, Simulink models, or MEX-files.

        % Example: scriptname = 'myScript'

        % Example: scriptname = 'anotherScript.m'

        % Example: scriptname = 'oneMoreScript.mlx'

       (scriptname是绝对的或相对的脚本文件路径,是一个字符串,可以指定MATLAB的任意文件类型。)

Tips:

run executes scripts not currently on the MATLAB path. However, you should use cd or addpath to navigate to or to add the appropriate folder, making a script executable by entering its name alone.scriptname can access any variables in the current workspace.run changes to the folder that contains the script, executes it, and resets back to the original folder. If the script itself changes folders, then run does not revert to the original folder, unless scriptname changes to the folder in which this script resides.If scriptname corresponds to both a .m file and a P-file residing in the same folder, then run executes the P-file. This occurs even if you specify scriptnamewith a .m extension.If a script is not on the MATLAB path, executing the run command caches the script. In the same session and after calling run, you can edit the script using an external editor. Call clear scriptname before calling run again to use the changed version of the script rather than the cached version. If you edit the script with the MATLAB editor, run executes the changed version and there is no need to call clear scriptname.

译:

run 执行不在MATLAB当前路径上的脚本。但是,您应该使用cd或addpath导航到或者添加相应的文件夹,通过输入其名称,使脚本可执行。scriptname 可以访问当前工作空间中的任何变量。run 切换至包含该脚本的文件夹,再执行该脚本,完毕后重新回到原始文件夹。如果脚本本身(内部)更改了文件夹,则 run 不会还原到原始文件夹,除非是用 scriptname 来切换的此脚本所在的文件夹。如果 scriptname 对应于同一文件夹中的.m文件和P文件,则 run 执行P文件。即使您指定带有.m扩展名的scriptname,也会发生这种情况。(即:如果同一文件夹下包含同名的.m文件和P文件,则优先执行P文件。)如果脚本不在MATLAB路径上,则执行 run 命令会缓存脚本。在同一会话中以及调用 run 之后,您可以使用外部编辑器编辑脚本。在再次调用run之前调用clear scriptname,来使用更改的脚本版本而不是缓存版本。如果使用MATLAB编辑器编辑脚本,则 run 执行更改的版本,无需调用clear scriptname。

附:run函数的在线帮助文档:https://ww2.mathworks.cn/help/matlab/ref/run.html

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3