0

I'm trying to get the names of inputs and outputs of a function that I've defined while outside the function. In other words, I can't make any change inside the function. I could not find any built-in function that does this. Is there a function that gives details or summary of a function.

For example, myfunc.m file is like below and I'm calling this function in another script.

function [out1, out2] = myfunc(input1, input2, input3)
operations
end

I need to get the strings 'out1', 'out2', 'input1', 'input2', 'input3'

I'm using Matlab R2018a.

alpereira7
  • 1,522
  • 3
  • 17
  • 30
lol 2
  • 1
  • 2
  • 3
    For inputs: [`inputname`](https://www.mathworks.com/help/matlab/ref/inputname.html). For outputs I don't know. See also [`functions`](https://www.mathworks.com/help/matlab/ref/functions.html). But I consider it bad practice if a function wants to know the name of its input or output arguments. Do you really need that? – Luis Mendo Jan 14 '19 at 10:47
  • I need to get names of inputs and outputs from outside of the function – lol 2 Jan 14 '19 at 10:55
  • and if I call it like this: [outA, outB] = myfunc(inputA, inputB, inputC), do you still want 'out1', 'out2', 'input1', 'input2', 'input3'? – Yuval Harpaz Jan 14 '19 at 14:02
  • yes, it works but I should not make any change in the function as I said. – lol 2 Jan 15 '19 at 05:53
  • 3
    Possible duplicate of [How do I retrieve the names of function parameters in matlab?](https://stackoverflow.com/questions/10431577/how-do-i-retrieve-the-names-of-function-parameters-in-matlab) – Sardar Usama Jan 15 '19 at 08:19
  • 2
    Please share **why** you need this. This is highly uncommon practice, and likely you *actual* problem is easier to solve without this step. – Cris Luengo Jan 15 '19 at 14:07
  • Dear Sardar Usama, the code in the link that you gave has worked. Thank you so much. I could not find how to accept your comment as the most helpful answer. – lol 2 Jan 25 '19 at 08:28

2 Answers2

0

You can use inputname for the input, for output there is no straightforward solution but you can try this

Yuval Harpaz
  • 1,416
  • 1
  • 12
  • 16
  • Can I learn inputs of the function from outside of the function? I don't want to change the function.. – lol 2 Jan 14 '19 at 11:01
  • before running the function or while it is running? before, the only thing to do is analize the code .m file. while it is running you can use evalin or asignin to send thing in or get stuff out, but his is a shaky thing to do. You can share the function, it'll help us understand what you are trying to do – Yuval Harpaz Jan 14 '19 at 12:04
  • I have updated the question, please look at that again. – lol 2 Jan 14 '19 at 12:49
0

If your allowed to make changes in the myfunc.m file, I can suggest you try something like:

function [out1, out2] = myfunc(input1, input2, input3)
% 'out1', 'out2', 'input1', 'input2', 'input3'

operations

end

Then in the other script (e.g. test.m):

% stuff...
help myfunc
% things....

It will return

'out1', 'out2', 'input1', 'input2', 'input3'


If your allowed to make changes in the myfunc.m file, you can also try:

function [out1, out2, string] = myfunc(input1, input2, input3)

operations

string = ["out1", "out2", "input1", "input2", "input3"];

end

And in the script

[~,~,string] = myfunc(1, 1, 1)

It will return:

string = 
1×5 string array

    "out1"    "out2"    "input1"    "input2"    "input3"

EDIT: Since your edits, you are not allowed to intervene in the myfunc.m file, but you want to read the first line of the function, you can try commands open or edit.

In the other script, write open myfunc or edit myfunc and it will open the file in a new tab, and you will be able to read it.

alpereira7
  • 1,522
  • 3
  • 17
  • 30