【问题标题】:Passing (commandline?) arguments to a Matlab script将(命令行?)参数传递给 Matlab 脚本
【发布时间】:2016-12-19 19:45:36
【问题描述】:

只是一个非常简单的问题。

我已收到此文件,我想运行它。

我相信它需要命令行参数才能与function trees_main(puzzle_ind,print_f,nls) 一起工作,但我不确定。它是几个不同文件的一部分 - 但这是主控制器。

关于如何向函数传递一些参数的任何想法?是在运行时完成的吗?

    % A* search tree algorithm for solving 8 puzzle problem
    % Inputs:
    %   puzzle_ind = 1:30
    %   print_f = 0/1, 1 to show the step solutions 
    %   nls = 0/1, 1 to Nilsson Score
    % Calls: 
    %   trees (A* search)
    %   trees_gm (Generate moves)
    %   trees_mh (Manhattan metrics)
    %   trees_nls (Nilsson sequence)
    % Uses:
    %   combinations.mat (30 resolvable puzzle combinations)
    %

    function trees_main(puzzle_ind,print_f,nls)

    global Tg T0 T1 nls_f % These variables are visible in all functions
    nls_f = nls;
    Tg = [1 2 3; 8 0 4; 7 6 5]; % Goal sequence
    % Coordinates (row and column) of tiles in a puzzle
    T0 = [1 1; 1 2; 1 3; 2 1; 2 2; 2 3; 3 1; 3 2; 3 3]; 
    T1 = [1 2 3 8 0 4 7 6 5];
    load combinations Cmb

    if puzzle_ind >= 1 && puzzle_ind <= 30 
      P = Cmb{puzzle_ind};  % assign the Puzzle
      [T,n1,n2] = trees(P,print_f); % call A* search function
      fprintf('\nPuzzle:\n')
      fprintf('% 1i %1i %1i\n',Cmb{puzzle_ind}')
      fprintf('Solution:\n')
      fprintf('% 1i %1i %1i\n',T') 
      fprintf('Number of nodes = %4i\nNumber of moves = %2i\n\n',n1,n2)
    end
    trees_plot(puzzle_ind); % statistics
    return

    function trees_plot(pind)
    global N1 nls_f
    M = (N1(:,[2 8])');
    n = size(M,2);
    plot(1:n,M)
    xlabel('Number of Tested Nodes')
    ylabel('Metrics (without cost)')
    if nls_f == 0
      s1 = 'No';
    else
      s1 = 'Yes';
    end
    title(sprintf('Puzzle Index %2i, NSS %s',pind,s1))
    axis([1 n 0 max(M(1,:))+1])
    grid on
    text(5,15,'Level (Cost)')
    return

感谢您的宝贵时间

【问题讨论】:

  • 你在网上搜索过这个吗? MATLAB 有很好的文档,这是一个非常基本的问题。

标签: matlab function command-line parameters


【解决方案1】:

如果你想从 matlab 命令行运行这个函数 trees_main(puzzle_ind,print_f,nls),那么它应该用文件名 trees_main.m 保存在你的matlab路径。您可以在此文件 (trees_main.m) 所在的位置使用 addpath('some path')。现在,您已准备好从命令窗口调用。就像普通函数调用一样调用。 例如,trees_main(15, 1, 1)。

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 2013-10-22
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多