How to access and change variables in workspace
Afficher commentaires plus anciens
I need to access variables in a workspace which are in a workspace down the stack. And I need to do this in a function - Thus, start the debugger, select the workspace manually and change the variable is not the thing I want to do. Thus, I need to do something like
evalin( 'caller', evalin( 'caller', evalin( ..., variablename) ) ... ).
I am open for hacky solutions too.
Furthermore: Please do not tell me any of the following: "Do not use eval", "Do not use global variables", "Don't do this" etc.. I am not a novice programmer (and hate that in similar questions the answer is never given, but only told that one should not do it).
7 commentaires
John D'Errico
le 30 Nov 2020
Based on evalin is not a hacky solution? :)
You can choose to call yourself not a novice programmer. But if you put yourself into the situation where the only solution is a tool used mainly by novices, then what might you expect?
tommsch
le 30 Nov 2020
Steven Lord
le 30 Nov 2020
You said not to tell you "Don't do this", but the design you've chosen to me seems pretty inefficient. Can you say a little more about why you feel you need to do it this way rather than passing data around through either input/output arguments or nested functions?
With this approach you're fixing not only what the function that uses this code does, but also from where it is allowed to be called. That could have implications for reusability of this code as well as its testability.
tommsch
le 30 Nov 2020
Image Analyst
le 30 Nov 2020
How does your program, deep within the stack, even know the variable name that it wants to access? I mean how does it know it wants to use variableName, and why does it want to use it? If the user or test program writes some program with arbitrary variable names, then how are you going to know which ones they created are the ones you want to use? Like if they made variables a,b,c,x,y,z, then how does your program know that it needs to use y when whatever y represents (could be amplitude, or weight, or distance, or whatever) could have been given a different name by the caller than the "y" you're trying to use?
Not sure it helps, but on a wild guess, look at "inputname" in the help.
Steven Lord
le 30 Nov 2020
This is for a testing framework, in particular for testing legacy code.
How exactly is retrieving data from workspaces lower in the stack going to help with your testing framework? Your test functions are going to call the software under test. For it to have access to variables from the subfunctions you'd have to have the test function executing at the same time as the subfunction and have that test function access the callee's workspace not the caller's.
Réponses (0)
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!