Matlab - how to retrieve the exact parameter list of the function call as a string inside the function?

前端 未结 1 1881
盖世英雄少女心
盖世英雄少女心 2021-01-24 09:35

Assume I have the function:

function name_the_paramlist(varargin)
    % Print out varargin exactly how it is called as a string

Basically what

相关标签:
1条回答
  • 2021-01-24 10:22

    Well, if you type this function call in a command line or run with F9 (so it get saved in the history) you can read the history.m file and get the last command as a string.

    fid = fopen(fullfile(prefdir,'history.m'),'rt');
    while ~feof(fid)
        lastcmd = fgetl(fid);
    end
    fclose(fid);
    

    Then get the argument part:

    arg_str = regexp(lastcmd, '\w+\((.+)\)','tokens','once');
    arg_str = strrep(arg_str{:},'''','''''');
    

    UPDATE:

    Another idea. If you call this function from another script or function (m-file) you can use DBSTACK to return the m-file name and the current line number:

    SI = dbstack;
    filename = SI(end).file;
    lineNo = SI(end).line;
    

    Then you can follow similar technique as in the first solution to read that line from the m-file and get argument part.

    In opposite to the first solution this won't work if run from command line or editor cell mode.

    0 讨论(0)
提交回复
热议问题