• Matlab调用可执行程序、传递参数以及接收输出


    简介

    这里介绍的方法对于C/C++和Fortran等编译的可执行程序都适用。有两种方式可将参数传递给程序,大家根据需要进行选择即可。

    C++的main函数参数

    C++的main函数可以没有输入参数,也可以有输入参数,而且只能有两个参数,习惯上coding如下:

     int main(int argc, char* argv[]) 或者 int main(int argc, char** argv)
    
    • 1

    其中,
    argc = argument count :表示传入main函数的数组元素个数,为int类型
    argv = argument vector :表示传入main函数的指针数组,为char**类型。
    第一个数组元素argv[0]是程序名称,并且包含程序所在的完整路径。argc至少为1,即argv数组至少包含程序名。

    #include 
    using namespace std;
    int main(int argc, char* argv[])
    {
        for(int i=0;i<argc;i++)
            cout<<argv[i]<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    一般编译器默认使用argc和argv两个名称作为main函数的参数,但这两个参数如此命名并不是必须的,你可以使用任何符合C++语言命名规范的变量名,但要保证第一个参数类型为int型,第二个参数为char**型,如下图所示。

    #include 
    using namespace std;
    int main(int count, char* input_parameters[])
    {
        for(int i=0;i<count;i++)
            cout<<input_parameters[i]<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    由于main函数不能被其他函数调用,因此不可能在程序内部取得实际值。main函数的参数值是从操作系统命令行上获取的。在window系统中,假如编译链接成的可执行文件为my_project.exe,则在命令提示符(快捷键windows+R,输入cmd)中,键入如下命令(可执行文件 参数 参数 参数 …),如:

    my_project.exe xiaoming 1996
    
    • 1

    将会传递三个参数给main函数,第一个argv[0]是前面提到的文件名,第二个argv[1]是"xiaoming",第三个argv[2]是“1996”。同理,可以传入更多的参数。在ubuntu系统中,可以通过终端进行相同的操作。

    传入的参数数组类型为char *字符串类型,可以通过atoi,atof函数进行类型的转换。

    atoi, 即ascii to integer,把字符串转换成int
    atof, 即ascii to float,把字符串转换成double
    atol, 即ascii to long int,把字符串转换成long int
    atoll,即ascii to long long int,把字符串转换成long long int
    
    • 1
    • 2
    • 3
    • 4

    例如上述输入的1996,可以得到如下:

    int year = atoi(argv[2]);  // year = 1996
    
    • 1

    因此,通过上述的命令行输入以及程序里面的类型转换,可以通过命令行窗口传入值(字符串和数字)到程序中运行。

    Matlab调用可执行C/C++程序、传递参数以及接收输出

    Matlab system函数

    status = system(command)
    [status,cmdout] = system(command)
    [status,cmdout] = system(command,'-echo')
    
    • 1
    • 2
    • 3

    说明
    status = system(command) 调用操作系统执行指定的命令。操作会等待命令执行完毕,然后再将命令的退出状态返回到 status 变量。'-echo’选项将程序输出回显到命令窗口。

    status — 命令退出状态
    0 | 非零整数
    命令退出状态,以 0 或非零整数的形式返回。当命令成功执行时,status 为 0。否则,status 为非零整数。

    cmdout — 操作系统命令的输出
    字符向量
    操作系统命令的输出,以字符向量形式返回。

    方法1——测试程序

    #include 
    
    double add(double a, double b);
    
    int main(int argc, char* argv[])
    {
    	for (int i = 0; i < argc; ++i)
    	{
    		std::cout << "Num:" << argc << " Par:"<< argv[i] << std::endl;
    	}
    
    	if (argc != 3)
    	{
    		return 0;
    	}
    
    	double a = atof(argv[1]);
    	double b = atof(argv[2]);
    
    	std::cout << add(a, b);
    	return 0;
    }
    
    double add(double a, double b){
    	return a + b;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    编译后生成add.exe可执行程序,在Matlab中通过代码调用:

    clc; clear; close all
    [stus, cmdout] = system('add.exe 8 7')
    
    • 1
    • 2

    输出结果:

    stus =
    
         0
    
    cmdout =
    
        'Num:3 Par:add.exe
         Num:3 Par:8
         Num:3 Par:7
         15
         '
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    stus 为0表示成功调用可执行程序,cmdout中保存着可执行程序里输出的内容,从中提取自己需要的结果即可。

    提示: 如果可执行程序里需要从键盘输入参数,那么Matlab调用后也需要在命令窗口进行输入,或者通过方法2。

    方法2——测试程序

    #include 
    
    double add(double a, double b);
    
    int main()
    {	
    	double a;
    	double b;
    
    	std::cin >> a >> b;
    
    	std::cout << add(a, b);
    	return 0;
    }
    
    double add(double a, double b){
    	return a + b;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Matlab调用方式,该方式需要把输入参数写入临时文件,再将临时文件传入可执行程序即可,更加方便。

    clc; clear; close all
    
    a = 13;
    b = 12;
    parameters = [a b]'; 
    dlmwrite('parameters.txt',parameters) %将参数写入临时文件传入可执行程序
    [stus, cmdout] = system('test_add_exe.exe  < parameters.txt','-echo');
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Fortran程序带参数运行方法

    方法1

    通常情况下基于控制台的Fortran源代码在编写完毕后,生成用工程名命名的可执行文*.exe,运行该exe就能执行Fortran程序。但是,有时候我们需要在命令提示符下输入可执行文件名后面添加若干个参数,希望把这些参数传递到Fortran程序中去执行。采用get_command_argument函数,用法:CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])其中NUMBER是获取第几个参数,VALUE是相应的值(让NUMBER=0得到的是可执行程序的名字,如果输入参数个数比NUMBER小,得到的为空。);LENGTH是第NUMBER个参数的长度;STATUS是获取这个参数后的状态(如果取这个参数发生错误,返回的是正数;如果VALUE是一个截断的参数,那么返回-1;其它情况返回0)。

    Fortran数据类型转换例子:

      integer*4 iyr,imo,idy,ihr,imn
      real*8 glad,glod,sec
      character(len=32) temp;
      
      call get_command_argument(1,temp)
      read(temp,*) iyr
      call get_command_argument(2,temp)
      read(temp,*) imo
      call get_command_argument(3,temp)
      read(temp,*) idy
      call get_command_argument(4,temp)
      read(temp,*) glad
      call get_command_argument(5,temp)
      read(temp,*) glod
      call get_command_argument(6,temp)
      read(temp,*) ihr
      call get_command_argument(7,temp)
      read(temp,*) imn
      call get_command_argument(8,temp)
      read(temp,*) sec
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Fortran代码

      program test_solid
      integer*4 i,n
      character(len=32) arg1,arg2
      call get_command_argument(1,arg1)
      print *,arg1
      call get_command_argument(2,arg2)
      print *,arg2
      read(arg2,*) n
      print *,arg2
      end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    终端输出

    $ ./test_f.exe Hello 123.31223
     Hello
     123.31223
     123.31223
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    注意: 需要程序本身可以独立运行。

    方法2

          program test_solid
          implicit double precision (a-h,o-z)
          integer*4 n
          character(len=32) arg1
    	  write(*,'(a$)')
          read(*,*) arg1
    	  write(*,'(a$)')
          read(*,*) n
    	  print *,arg1,n
          end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Matlab测试

    clc; clear; close all
    
    a = 13;
    b = 12;
    parameters = [a b]'; 
    dlmwrite('parameters.txt',parameters) %将参数写入临时文件传入可执行程序
    [stus, cmdout] = system('test_f.exe  < parameters.txt','-echo');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    自定义vue项目的雷达图组件
    《LeetCode力扣练习》代码随想录——链表(两两交换链表中的节点---Java)
    Python3:PyArg_ParseTuple与Py_BuildValue
    LeetCode994.腐烂的橘子
    systemverilog学习 --- casting and local protected
    PDF软件PDF Extra Premium + Ultimate 9.30.56026
    设计模式-代理模式-静态代理和动态代理在Java中的使用示例
    JDK的配置及运行过程
    在用户同意隐私政策前,您的应用获取了用户的ANDROID ID,不符合华为应用市场审核标准。
    山峰和山谷—BFS
  • 原文地址:https://blog.csdn.net/wokaowokaowokao12345/article/details/126937873