Welcome! Log In Create A New Profile

Advanced

Use of Include using variable input

Posted by CptCook95 
Use of Include using variable input
July 09, 2025 05:14PM
Hi Jaewon,

This may be an odd inquiry, but I am attempting to use include('a_function.m') to enable a function to use the ML library so they can display graphics.

This works if I include each function manually, but I cannot do so through a variable, even if I evaluate the variable as a string within include. For example include(eval(functionVariable)) where functionVariable is a char array 'task1.m'.

I would like to be able to include these functions in a variable way if possible, since it will help simplify my code and make it scalable in the future. I suspect it is not possible to do this since ML creates a runtime script that it can't modify during runtime, but please let me know if you have any idea of the feasibility of this.

Additionally, I want to express my appreciation for your work and thank you for always helping out on the forums.
Re: Use of Include using variable input
July 09, 2025 09:10PM
You are trying to add a new variable to a static workspace. It does not work anywhere in MATLAB.

You cannot insert a nested function dynamically, but you can define them all first and choose one dynamically.
include('func1.m', 'func2.m', 'func3.m');  % each script must be a function.
eval('func1');

I am glad to hear that NIMH ML is useful for you.

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.