MATLAB Answers

Debugging a nested function

53 views (last 30 days)
Peter Borda
Peter Borda on 25 Apr 2015
Edited: Elmar Zander on 14 Nov 2018
Hello!
I have created a code with nested functions in it. My problem is, that when I am in debugging mode and try to create a variable for testing certain values, matlab gives the following error:
Attempt to add "test" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
It is really bothering, because I create test variables all the time during debug mode. Is there a solution for that, where I can do so?

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Apr 2015
Peter - see variables in nested and anonymous functions for a description of the problem and possible solutions (the suggestion is to use global variables). On the other hand, why do you want to create new variables? Can't you just use the Command Window to display the current state/value of the variables?

  3 Comments

Shai Machnes
Shai Machnes on 13 Nov 2017
Sorry but stupid, possibly rude, answer, because you're telling the person they don't really want to do what they really want to do.
It is often very convenient to do some temporary calculation to verify code. Or maybe code part of the function in the context of the function and run the new code with F9. There are tons of legitimate reasons to define variables in a "static workspace".
Jan
Jan on 13 Nov 2017
@Shai Machnes: I do not see any rudeness here. Geoff does not tell, what to do, but asks if the possible interactions are not sufficient already. In addition he explained the reason of the limitation and the linked page contains a valid solution: Create a dummy variable in the parent of the nested function. Then you can assign a value without creating a variable dynamically.
Shai Machnes
Shai Machnes on 13 Nov 2017
I have functions which contain the following opening lines:
% Defined loads of variables so they are available to you for debugging at breakpoints (and no more "adding variable to static workspace" crap)
a = 0; b = 0; c = 0; d = 0; f = 0; g = 0; h = 0; j = 0; k = 0; l = 0; m = 0; n = 0; o = 0; p = 0; q = 0; r = 0; s = 0; t = 0; u = 0; v = 0; w = 0; x = 0; y = 0; z = 0;
A = 0; B = 0;C = 0; D = 0; E = 0; F = 0; G = 0; H = 0; I = 0; J = 0; K = 0; L = 0; M = 0; N = 0; O = 0; P = 0; Q = 0;R = 0; S = 0; T = 0; U = 0; V = 0; W = 0; X = 0; Y = 0; Z = 0;
If the language forces you to do stuff like that, there is something broken in its design.

Sign in to comment.

More Answers (1)

Elmar Zander
Elmar Zander on 14 Nov 2018
Edited: Elmar Zander on 14 Nov 2018
The problem mostly appears, when you want to do something a bit more sophisticated in debugging, e.g. calling functions with more than one return value where you need both of them. What you can do in this case is usually two (or three) things:
a) Create a function just for debugging purposes: let's say you have an array arr and want to get the minimum and the corresponding index. Then just doing
[min_val, min_ind]=min(arr)
will give you the dreaded "Attempt to add ..." error. So, create a function, say stupid_debugging_function and in it you just place a keyboard command, like this
function stupid_debugging_function(arr)
keyboard
Then you call stupid_debugging_function(arr) from the place you wanted to debug the contents of arr and inside stupid_debugging_function you can do whatever you want, since it is no static workspace.
You can always keep your stupid_debugging_function around and just quickly adapt the arguments with some copying and pasting, if you need more variables in your debugging session.
b) Second solution: you can always add the ans variable to a static workspace, since it is a rather special beast. Now you can either do
ans=cell(2,1);
[ans{:}]=min(arr)
or even easier
ans={}
[ans{1}, ans{2}]=min(arr)
and you can put more results at the end of ans like this
ans{3} = ans{1} * ans{2} % or
ans{end+1} = exp(ans{end})
c) If it is only about getting e.g. the second (or third, or fourth...) output argument of a function simply use ans and the tilde ~
[~, ans] = min(arr)

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by