1

I have a project consisting of multiple nested functions. For debugging purpose I want to save all internal variables in one way or another, in order to display figures, replay parts of code etc...
I also want to keep this as transparent as possible regarding calculation time.

My first thought was to create a global variable, and store programmatically at the end of each function the inputs and outputs inside the variable as a structure :

globalVariable.nameOfParentfunction_NameOfFunction.nameInput1 = valueInput1;
globalVariable.nameOfParentfunction_NameOfFunction.nameInput2 = valueInput2;
...
globalVariable.nameOfParentfunction_NameOfFunction.nameOutput1 = valueOutput1;
...

Is it possible to use some kind of reflection to get the name and value of inputs/outputs without necessarily parse the file where the function is written?

I found a good but maybe outdated topic about parsing

How do I collect internal signals?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Chewbaka
  • 184
  • 10
  • You'd likely be better off using `assignin` to assign the variables to your base workspace, but both this and the global variable approach seem very messy. If it's just for debugging, you'd likely be better debugging within specific functions. If it's data you want more broadly available to caller functions then just provide more outputs from your functions, i.e. maybe a 2nd output which is a struct of desired values. – Wolfie Nov 25 '21 at 14:32
  • This is actually the process used, a structure is passed as input/ output to store the workspace data with `ws2struct()`. I wanted to see if there way a better approach. I am not fond of creating input/output each time i must create a new function, as there are a lot of nested function and they may be called multiple times, but this is a point of view. – Chewbaka Nov 25 '21 at 14:58
  • 1
    Or make a `handle` class which you only have to pass as an input and update its properties – Wolfie Nov 25 '21 at 15:57

1 Answers1

3

The simple solution is to use save, which will save all variables in the current workspace (the function’s context) to file.

If you want to keep the values in memory, not in a file, you can use names = who to get a list of all variables defined in the current workspace, then use val = eval(names{i}) to get the value of the variable called name{i}.

I would recommend putting all of that in a separate function, which you can call from any other function to store its variables, to avoid repeating code. This function would use evalin('caller',…) to get names and values of variables in the workspace of the calling function.


Note that using eval or evalin prevents MATLAB from optimizing code using its JIT. They also are dangerous to use, since they can execute arbitrary code, however in this case you control what is being executed so that is no a concern.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 1
    This is a good method and seems to fit my needs, I let the question as unsolved a bit of time to wait for other methods. I'll implement it when time allows and post the subfunction. Thank you – Chewbaka Nov 25 '21 at 14:48