Debugging function code and workspace variable issues

20 visualizaciones (últimos 30 días)
Jared
Jared el 2 de En. de 2015
Comentada: Stephen23 el 2 de En. de 2015
Hi all,
Relatively new MATLAB user and I've written a couple of functions and have started debugging those functions. I'm a bit confused though by the behavior in debug mode compared to what I'm used to when debugging script files.
For example, when running my function code up to a breakpoint, MATLAB clears the workspace (or more accurately it looks like MATLAB changes the workspace from the general workspace to the function's workspace). If I need some of the variables from my general workspace as input arguments to the function, MATLAB won't let me copy those variables over to the function workspace after function debugging has started.
How do folks work around these issues (or am I doing something wrong here)?
Thanks,
JK

Respuesta aceptada

Stephen23
Stephen23 el 2 de En. de 2015
Editada: Stephen23 el 2 de En. de 2015
I am not sure if you can (easily) grab values from one workspace and pull them into the current function workspace... you might like to try dbup , which lets you change workspace while debugging. But variables only exist in one workspace, unless explicitly specified otherwise (e.g. global). It might be possible to do some major hackery using assignin and evalin and the like, or perhaps even calling global, but this would not be beautiful...
You are correct that when using the debugger (on a function) the workspace shown in the IDE changes from the "base" workspace to that of the (normally hidden) function itself. All of the variables that you have defined within the function are shown there, including the ones that are inputs to the function. Thus the standard way to get variables from the base workspace into a function workspace is to call that function with those variables as input arguments, as the workspaces are kept quite separate.
This page covers all of this in much more detail:
Note that scripts do not have their own workspace, as they operate in the base workspace.
Anyway, my answer for what it is worth: the simplest solution would be to change the function by adding a new argument, and then run it again.
  1 comentario
Stephen23
Stephen23 el 2 de En. de 2015
Actually this works:
function temp(x)
disp(size(x))
end
Save and set break-point on the second line. Then call it from the command line:
>> temp(1:3)
>> dbup
>> global y
>> y = 3;
>> dbdown
>> global y
Bingo! Variable y is now available in the function workspace. But honestly, it would be easier just adding a new input and calling the function again...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Timing and presenting 2D and 3D stimuli en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by