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?
Peter - seevariables 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?
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".
@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.
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.
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 ~
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
0 Comments
Sign in to comment.