0

Is there a way to grab the name of a structure that is passed into a function in matlab from within the new function.

Example:

y = fnX(StructName) % call function



function[y] = fnX(name)

% here is where I want to capture StructName as char text.

end

It seems that the structure is renamed and the original name is lost within the function. Any suggestions?

2 Answers 2

2

The best suggestion I can give, is not to depend on the name of a variable.

It is possible by means of inputname, but it would be much more natural to let the name, or any information that you are interested in, be stored in the variable. For example by adding a field to the struct.

This should allow for more code flexibility and reusability and probably improve readability in the meantime.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use inputname.

function[y] = fnX(name)

% here is where I want to capture StructName as char text.
StructName = inputname(1);

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.