Alternatives to using eval/evalc for running a file?

42 views (last 30 days)
Hello, I understand that the eval family of functions does not work as efficiently and effectively as other alternatives in many cases. Although I did find alternatives in both Matlab documentation and in other forumn pages, this seemed to relate more to the issue of someone using eval to do this:
x = eval('VariableName');
I understand how using a structure would work better in this instance. In my program, however, I am running a Matlab script within a separate function. Currently, I am doing this (the 'x =' simply suppresses output to the command window):
X = evalc('FileNameOfTheScript');
I wanted to ask if there is a more efficient way for me to write this code, while still having it do the same task (add the variables called in the script to the workspace).
Thanks, Ak
  1 Comment
Adam
Adam on 10 Jul 2018
Edited: Adam on 10 Jul 2018
The answer added covers the best way to deal with this, but on a related note, you need to be clear which workspace you are referring to. Every function has its own self-contained workspace. For new users this feels very inconvenient and unwanted, but for those more used to it it is part of the essence of why functions are so much a better way to program than scripts. The base workspace is the exception which is always there, but you shouldn't generally be working in when running a program, it is more for command line work or early scripts to test out ideas quickly.
You should always be seeking to keep a tight control on the scope of the variables you use. If they need to move from one scope to another then you can return them as function arguments (or use handle-derived classes, but that is more advanced) when you make a function call.
Having access to all your variables in one place may seem nice for a while, but it soon causes plenty of headaches.

Sign in to comment.

Accepted Answer

Jan
Jan on 10 Jul 2018
If you have defined the name of the script to run as a variable, use the run() command. See doc run (link)
  1 Comment
Ak K
Ak K on 11 Jul 2018
Thank you, for some reason I never found that function in my research.

Sign in to comment.

More Answers (1)

OCDER
OCDER on 10 Jul 2018
Can we see the script? It should be able to be turned into a function instead. If you want the script to use the current variables of the workspace, you could just call the script name itself
>> FileNameOfTheScript
But it's best to turn this into a function, and AVOID using eval or evalc or evalin etc.
function X = MyFunction(In1, In2, In3)
X = In1 + In2 - In3;
>> X = MyFunction(In1, In2, In3)
"In my program, however, I am running a Matlab script within a separate function."
Instead, turn your script into a function. Calling scripts in function is often a bad choice because your "script" is poofing in variables that your function doesn't see, and your script is calling variables that your functions may not have defined yet. Here's a related issue where scripts were called within functions. See example where this becomes an issue:
  5 Comments
Ak K
Ak K on 13 Jul 2018
Hello, thank you for your feedback.
Currently, my function creates a temporary file, dumps certain lines of code into it, then proceeds to run the temp. file in order to add variables calculated in the temporary file to the in-function workspace. For this reason, I need to be running the script so that it overwrites certain values within the main function.
OCDER
OCDER on 13 Jul 2018
Can we see this main function? Are you generating scripts just to calculate something in the main function? Overriding variables intentionally means you'll have to track each variable carefully. A single typo can throw your entire program off and generate no error. You don't "need" to do this...
Try asking the forum something like "What's a better way to run many scripts in a function to override function variables?" and provide your main function. You'll get a lot of great help from experts.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!