How to execute a function when stored in a variable

3 次查看(过去 30 天)
I am trying to find a way to store instructions on executing a line of code in a variable, so that when a user loads in that variable, they can execute the line of code contained within the variable. A simple example of something I'd like to run is:
sprintf('example %s',localVariable)
So the user's code would look something like:
localVariable = 'foo';
loadedVar = load('file.mat','Var'); %where 'Var' is something along the lines of @eval('sprintf(''example %s'',localVariable)'), but I can't seem to figure out exactly how to do this
a = loadedVar %I think I want loadedVar to be a function handle of some sort, where the function looks at the local workspace for 'localVariable' and constructs a string;
The desired output then when calling 'a' is:
'example foo'
Any ideas on how to accomplish this?

回答(2 个)

Jan
Jan 2021-6-29
编辑:Jan 2021-6-29
This is the job of functions. Hiding the function as char vector in a MAT file is too strange to be clear or efficient. Simple store the funtion in an M file and call it.
Do not reinvent Matlab as a programming language based on interpreting the contents of MAT files.
  3 个评论
Stephen23
Stephen23 2021-6-29
编辑:Stephen23 2021-6-29
" understand that I can manipulate this in each piece of code that references this variable..."
Passing the required data as input/output arguments would be the best solution.
"...but this would be enormousy tedious!"
It is not "tedious" to pass input/output arguments reliably (in fact it lets you concentrate on more important things).
In contrast the approach you have chosen obfuscates the code intent, and forces you into writing complex, inefficient, slow code which is liable to bugs and difficult to debug. That is a very... interesting design decision.
Dan Klisiewicz
Dan Klisiewicz 2021-6-29
A couple of notes:
Thanks for your suggestion and elaborating on input/output argument solution. It is most likely the solution I will move forward with.
Why would you comment on how tedious a task would be if you haven't seen the breadth of the code I'm dealing with?
You are correct, the approach in question is complex and most likely ineffecient - but these are some things you have to consider when weighing how long it would take to implement a simpler, more efficient solution vs. the need for a particular task to be completed and the level of risk adding such complexity would add. There's nothing wrong with exploring different solutions. That being said, this is not a case of the XY problem as the end user knows exactly what could be done to fix X.
"The inability of the support personnel to resolve their real problem or to understand the nature of their inquiry may cause the end user to become frustrated. The situation can make itself clear if the end user asks about some seemingly inane detail which is disconnected from any useful end goal. The solution for the support personnel is to ask probing questions as to why the information is needed, in order to identify the root problem and redirect the end user away from an unproductive path of inquiry. "
The support person WAS able to offer support to resolve the real problem and the support person did not have to ask probing questions as to why the information was needed. You'll notice that in my response I didn't say "how do I achieve not touching every piece of code that comes in contact with this variable". This is because I knew very well ahead of time what could be done to resolve X. Y came along as an alternate (borderline inane) solution that came about in my explorations that led me to try to understand Matlab's mechanisms better.
I appreciate Jan's suggestion and only replied because I needed clarification on one of his statements. I offered some context to show that I've thought about my approach in different ways. There was no frustration until you accused me of offering an unproductive path of inquiry.
Please stick to constructive comments such as "Passing the required data as input/output arguments would be the best solution." and leave it at that.

请先登录,再进行评论。


Asmit Singh
Asmit Singh 2021-6-29
I hope eval, feval documentaion migh come handy. Did not exactly get the question.
  1 个评论
Dan Klisiewicz
Dan Klisiewicz 2021-6-29
In my description I mention trying to use eval, but not being able to quite figure out the syntax, hence why I asked the question. This type of comment is not helpful at all.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by