Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:41:04+00:00 2026-06-04T19:41:04+00:00

Aside from parsing the function file, is there a way to get the names

  • 0

Aside from parsing the function file, is there a way to get the names of the input and output arguments to a function in matlab?

For example, given the following function file:

divide.m

function [value, remain] = divide(left, right)
     value = floor(left / right);
     remain = left / right - value;
end

From outside the function, I want to get an array of output arguments, here: ['value', 'remain'], and similarly for the input arguments: ['left', 'right'].

Is there an easy way to do this in matlab? Matlab usually seems to support reflection pretty well.

EDIT Background:

The aim of this is to present the function parameters in a window for the user to enter. I’m writing a kind of signal processing program, and functions to perform operations on these signals are stored in a subfolder. I already have a list and the names of each function from which the user can select, but some functions require additional arguments (e.g. a smooth function might take window size as a parameter).

At the moment, I can add a new function to the subfolder which the program will find, and the user can select it to perform an operation. What I’m missing is for the user to specify the input and output parameters, and here I’ve hit the hurdle here in that I can’t find the names of the functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:41:06+00:00Added an answer on June 4, 2026 at 7:41 pm

    If your problem is limited to the simple case where you want to parse the function declaration line of a primary function in a file (i.e. you won’t be dealing with local functions, nested functions, or anonymous functions), then you can extract the input and output argument names as they appear in the file using some standard string operations and regular expressions. The function declaration line has a standard format, but you have to account for a few variations due to:

    • Varying amounts of white space or blank lines,
    • The presence of single-line or block comments, and
    • Having the declaration broken up on more than one line.

    (It turns out that accounting for a block comment was the trickiest part…)

    I’ve put together a function get_arg_names that will handle all the above. If you give it a path to the function file, it will return two cell arrays containing your input and output parameter strings (or empty cell arrays if there are none). Note that functions with variable input or output lists will simply list 'varargin' or 'varargout', respectively, for the variable names. Here’s the function:

    function [inputNames, outputNames] = get_arg_names(filePath)
    
        % Open the file:
        fid = fopen(filePath);
    
        % Skip leading comments and empty lines:
        defLine = '';
        while all(isspace(defLine))
            defLine = strip_comments(fgets(fid));
        end
    
        % Collect all lines if the definition is on multiple lines:
        index = strfind(defLine, '...');
        while ~isempty(index)
            defLine = [defLine(1:index-1) strip_comments(fgets(fid))];
            index = strfind(defLine, '...');
        end
    
        % Close the file:
        fclose(fid);
    
        % Create the regular expression to match:
        matchStr = '\s*function\s+';
        if any(defLine == '=')
            matchStr = strcat(matchStr, '\[?(?<outArgs>[\w, ]*)\]?\s*=\s*');
        end
        matchStr = strcat(matchStr, '\w+\s*\(?(?<inArgs>[\w, ]*)\)?');
    
        % Parse the definition line (case insensitive):
        argStruct = regexpi(defLine, matchStr, 'names');
    
        % Format the input argument names:
        if isfield(argStruct, 'inArgs') && ~isempty(argStruct.inArgs)
            inputNames = strtrim(textscan(argStruct.inArgs, '%s', ...
                                          'Delimiter', ','));
        else
            inputNames = {};
        end
    
        % Format the output argument names:
        if isfield(argStruct, 'outArgs') && ~isempty(argStruct.outArgs)
            outputNames = strtrim(textscan(argStruct.outArgs, '%s', ...
                                           'Delimiter', ','));
        else
            outputNames = {};
        end
    
    % Nested functions:
    
        function str = strip_comments(str)
            if strcmp(strtrim(str), '%{')
                strip_comment_block;
                str = strip_comments(fgets(fid));
            else
                str = strtok([' ' str], '%');
            end
        end
    
        function strip_comment_block
            str = strtrim(fgets(fid));
            while ~strcmp(str, '%}')
                if strcmp(str, '%{')
                    strip_comment_block;
                end
                str = strtrim(fgets(fid));
            end
        end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Aside from having a pure virtual function, is there a way to prevent an
Aside from the GL Support, is there a way to override locale settings with
I do not know why there are 2 matches found aside from the input
I have two identical select boxes (aside from their names, of course), and I
Aside from keeping some kind of structured mark-up, is there any justifiable reason to
Aside from the you won't have to change the name, is there any real
Aside from using a hardware video encoding/decoding device, is there an easy was to
Given a function, I'm trying to find out the names of the nested functions
Aside from Chrome Developer Tools and Firebug in Firefox, are there other tools that
Aside from the HelloWorld Tutorial, are there any other tutorials/books available with regards to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.